tags:

views:

89

answers:

2

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.

+3  A: 

Try:

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

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).

.. http://ftp.sunet.se/pub/Linux/kernel.org/software/scm/git/docs/git-branch.html

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 br --no-color --verbose | \
sed -e 's/*//' | awk '{print $1}'`; \
do echo "[$branch]"; git co -q $branch; git br --merged; done

[broken]
* broken
[master]
* master
[next]
master
* next
[experimental]
master
next
* experimental
The MYYN
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
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
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
A: 

git showbranch 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.

Pat Notz