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.
Git Delete Branch: Do It When It's Time In general, it's safe to delete branches after they've been merged.
The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option. The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch. $ git branch -d release Deleted branch feature (was bd6903f).
They're unnecessary. In most cases, branches, especially branches that were related to a pull request that has since been accepted, serve no purpose. They're clutter. They don't add any significant technical overhead, but they make it more difficult for humans to work with lists of branches in the repository.
To delete all branches in Git except main, simply replace the grep for master with a grep for main: git branch | grep -v "main" | xargs git branch -D.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With