Delete all local git branches Delete all local git branches git git

Delete all local git branches


The 'git branch -d' subcommand can delete more than one branch. So, simplifying @sblom's answer but adding a critical xargs:

git branch -D `git branch --merged | grep -v \* | xargs`

or, further simplified to:

git branch --merged | grep -v \* | xargs git branch -D 

Importantly, as noted by @AndrewC, using git branch for scripting is discouraged. To avoid it use something like:

git for-each-ref --format '%(refname:short)' refs/heads | grep -v "master\|main" | xargs git branch -D

Caution warranted on deletes!

$ mkdir br$ cd br; git initInitialized empty Git repository in /Users/ebg/test/br/.git/$ touch README; git add README; git commit -m 'First commit'[master (root-commit) 1d738b5] First commit 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 README$ git branch Story-123-a$ git branch Story-123-b$ git branch Story-123-c$ git branch --merged  Story-123-a  Story-123-b  Story-123-c* master$ git branch --merged | grep -v \* | xargsStory-123-a Story-123-b Story-123-c$ git branch --merged | grep -v \* | xargs git branch -DDeleted branch Story-123-a (was 1d738b5).Deleted branch Story-123-b (was 1d738b5).Deleted branch Story-123-c (was 1d738b5).


I found a nicer way in a comment on this issue on github:

git branch --merged master --no-color | grep -v "master\|stable\|main" | xargs git branch -d

Edit: Added no-color option and excluding of stable branch (add other branches as needed in your case)


Parsing the output of git branch is not recommended, and not a good answer for future readers on Stack Overflow.

  1. git branch is what is known as a porcelain command. Porcelain commands are not designed to be machine parsed and the output may change between different versions of Git.
  2. There are user configuration options that change the output of gitbranch in a way that makes it difficult to parse (for instance, colorization). If a user has set color.branch then you will get control codes in the output, this will lead to error: branch 'foo' not found. if you attempt to pipe it into another command. You can bypass this with the --no-color flag to git branch, but who knows what other user configurations might break things.
  3. git branch may do other things that are annoying to parse, likeput an asterisk next to the currently checked out branch

The maintainer of git has this to say about scripting around git branch output

To find out what the current branch is, casual/careless users may have scripted around git branch, which is wrong. We actively discourage against use of any Porcelain command, including git branch, in scripts, because the output from the command is subject to change to help human consumption use case.

Answers that suggest manually editing files in the .git directory (like .git/refs/heads) are similarly problematic (refs may be in .git/packed-refs instead, or Git may change their internal layout in the future).

Git provides the for-each-ref command to retrieve a list of branches.

Git 2.7.X will introduce the --merged option to so you could do something like the below to find and delete all branches merged into HEAD

for mergedBranch in $(git for-each-ref --format '%(refname:short)' --merged HEAD refs/heads/)do    git branch -d ${mergedBranch}done

Git 2.6.X and older, you will need to list all local branches and then test them individually to see if they have been merged (which will be significantly slower and slightly more complicated).

for branch in $(git for-each-ref --format '%(refname:short)' refs/heads/)do    git merge-base --is-ancestor ${branch} HEAD && git branch -d ${branch}done