September 16, 2011

Cleaning up old git branches

Posted in Software at 20:14 by graham

After a while working with git, you end up with lots of branches, especially if you use git-flow inspired feature branches. Here’s one way to clean them up.

For any branch, I want to know whether it has been merged, when the last commit was, and ideally if the matching ticket in our tracker has been closed.

Switch to your main branch, usually develop or master: git checkout develop

List all the branches which have been fully merged into it:

git branch -a --merged

Show last commit date for a branch:

git log -1 --pretty=format:"%Cgreen%ci %Cred%cr%Creset" <branch_name>

Putting all that together, for all branches, gives us (thanks brunost and unixmonkey7740):

for k in $(git branch -a --merged|grep -v "\->"|sed s/^..//);do echo -e $(git log -1 --pretty=format:"%Cgreen%ci %Cred%cr%Creset" "$k")\\t"$k";done|sort|more

Most of my branches are named features/<ticket-id>, so I bring up the above list in one window, and the ticket tracker in a browser, and work through them.

To delete a branch:

# If you have it checked out locally
git branch -d <branch_name>

# Delete remote branch
git push origin :<branch_name>

To keep you safe from typos, git branch -d will refuse to delete branches that haven’t been merged.

Finally, once you have deleted all the old branches, your colleagues will need to bring their remote names in sync:

git remote prune origin

Remember that in git branches are just like symlinks, so you’re not losing any commits by deleting a branch, you’re just losing a named reference to a commit.

Happy pruning!

7 Comments »

  1. Vaclav Kosar said,

    June 18, 2016 at 10:53

    We use git-obsolete-branch-remover useful as it lists or removes local or remote Git branches based on last commit date and merge status. https://github.com/vackosar/git-obsolete-branch-remover

  2. Jomo Frodo said,

    July 22, 2014 at 18:06

    Thanks, this is helpful.

  3. starmonkey said,

    March 20, 2014 at 01:56

    You may also want to check out git-sweep – it’s up on github.

  4. Robbie said,

    March 27, 2013 at 18:50

    Thanks for the script!

    I’ve been using SourceTree and the branch list was starting to get a little long!

  5. Nuno Guerreiro said,

    December 4, 2012 at 13:52

    Great stuff! Saved me a lot of time.

  6. Padawin said,

    June 25, 2012 at 15:22

    Nice script, it will clearly help me.

    Instead of using your for loop, you can also do it like that: git branch –merged | grep -v remote | sed -e ‘s/\*//g’ | xargs -I{} git log -1 –pretty=format:”%Cgreen%ci %Cred%cr%Creset %d” {} | sort | more

  7. Lucy said,

    March 27, 2012 at 02:32

    Just a quick thank you for your script! I modified it for my own needs, but I wouldn’t have gotten there without your example. Thanks!

Leave a Comment

Note: Your comment will only appear on the site once I approve it manually. This can take a day or two. Thanks for taking the time to comment.