Can I delete this branch?

End-of-the-year housecleaning time: Our team’s project has accumulated a few* git branches that are almost certainly stale. Some of them were last worked on by people who aren’t even on the project anymore. Still, I wanted to reassure myself before I summarily deleted them. Here are the git commands I was able to round up. What’ve you got?

Start with git checkout master, since these work relative to the current branch and only make sense when that current branch is master.

git branch --merged
lists branches that have been merged to HEAD. (You’ve already made master the current branch, right?) If your branch is in that list, it’s in master and can be deleted.

git branch --contains my-branch-to-delete
lists branches that contain the branch you specified. If master shows up in the list, then master contains your branch.

git log my-branch-to-delete ^master --no-merges
lists commits that are in my-branch-to-delete but are not in master. The ^ means “exclude this branch”, so this is listing commits on my-branch-to-delete, excluding commits that are also on master. If there are no commits in this list, then master contains my-branch-to-delete.



* Not a few

About Sharon Cichelli

I am a Principal Technical Lead at Headspring, developing enterprise-changing software and coaching teams to deliver value without death marches. I am a .NET developer, open-source contributor, user group organizer, technical blogger, pinball fan, and Arduino enthusiast.
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • If you want a nuclear option, try this:

    alias gnuke=’git branch –merged | grep -v “*” | xargs -n 1 git branch –delete’

    Will automagically delete all of your local branches that have already been merged.