34

If you do git branch -d branchname, it will delete branch name if it references an earlier commit in your history, or tell you that you need to use -D otherwise. I often create branches that are later pushed to master, and can thus be deleted by this criterion. Is there an easy way to list all the branches that point to earlier commits of master, that is, branches that git will not mind deleting with just the -d option? Bonus points if it works for all branches at once, not just master.

I've got 82 local branches and I know that quite a few if not most of them can safely be deleted by now, but I don't want to take the time to go through each one to try to do it.

asmeurer
  • 86,894
  • 26
  • 169
  • 240

2 Answers2

42

Try:

$ git checkout master # or whatever branch you might compare against ...
$ git branch --no-merged
$ git branch --merged

From git branch documentation:

With --merged, only branches merged into the named commit (i.e. the branches whose tip commits are reachable from the named commit) will be listed. With --no-merged only branches not merged into the named commit will be listed. If the argument is missing it defaults to HEAD (i.e. the tip of the current branch).

EDIT:

to show this for every branch, you could do something like this:

example repo:

o <--- experimental
|
o
|
o <--- next
|
o
|
o <--- master
|
o----o <--- broken
|
o
|


$ for branch in `git branch --no-color --verbose | \
sed -e 's/*//' | awk '{print $1}'`; \
do echo "[$branch]"; git checkout -q $branch; git branch --merged; done

[broken]
* broken
[master]
* master
[next]
master
* next
[experimental]
master
next
* experimental
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
miku
  • 181,842
  • 47
  • 306
  • 310
  • I'm assuming you have br as a shortcut to branch :). That works. Now, how to do it for every branch, so I can get a good list of branches I can delete. Also, this shows remote branches, which I don't want. – asmeurer Mar 01 '10 at 04:32
  • Great. I figured it would require some shell script. I don't know sed or awk though, so I'm glad you wrote it. I think I will modify your script to not show the branch itself under each branch and to give the first line of the commit message for the HEAD of each branch, to make it easier for me to see what each branch is pointing to. Maybe the date of each commit too. – asmeurer Mar 01 '10 at 16:35
  • 2
    This could be improved (use `branch` instead of `br` and `checkout` instead of `co`, and make the script not show the same branch that you are on), but it did what I wanted, so I will mark it as the answer. – asmeurer Jun 18 '10 at 05:10
7

git show-branch is a little known but pretty useful tool that visually shows the commits that are unique to each branch. It can be hard to decipher at first but once you understand the output it's pretty usable. There's a brief but good introduction available.

DESCRIPTION

   Shows the commit ancestry graph starting from the commits named
   with <rev>s or <globs>s (or all refs under refs/heads and/or
   refs/tags) semi-visually.

   It cannot show more than 29 branches and commits at a time.
Community
  • 1
  • 1
Pat Notz
  • 208,672
  • 30
  • 90
  • 92