Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List git branches that can safely be deleted

Tags:

git

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.

like image 388
asmeurer Avatar asked Mar 01 '10 04:03

asmeurer


People also ask

Is it safe to delete git branches?

Git Delete Branch: Do It When It's Time In general, it's safe to delete branches after they've been merged.

How do I remove old branches in git?

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

Should you delete old branches?

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.

How do I delete all branches except master?

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.


2 Answers

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 
like image 153
miku Avatar answered Sep 18 '22 08:09

miku


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. 
like image 36
Pat Notz Avatar answered Sep 21 '22 08:09

Pat Notz