Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete all local git branches

I follow a development process where I create a new local branch for every new feature or story card. When finished I merge the branch into master and then push.

What tends to happen over time due to a combination of laziness or forgetfulness, is that I end up with a large list of local branches, some of which (such as spikes) may not have been merged.

I know how to list all my local branches and I know how to remove a single branch but I was wondering if there was a git command that allows me to delete all my local branches?

Below is the output of the git branch --merged command.

user@machine:~/projects/application[master]$ git branch --merged   STORY-123-Short-Description   STORY-456-Another-Description   STORY-789-Blah-Blah * master 

All attempts to delete branches listed with grep -v \* (as per the answers below) result in errors:

error: branch 'STORY-123-Short-Description' not found. error: branch 'STORY-456-Another-Description' not found. error: branch 'STORY-789-Blah-Blah' not found. 

I'm using:
git 1.7.4.1
ubuntu 10.04
GNU bash, version 4.1.5(1)-release
GNU grep 2.5.4

like image 387
Louth Avatar asked May 15 '12 23:05

Louth


People also ask

How do I delete all local branches?

Explanation: Get all branches (except for the master) via git branch | grep -v "master" command. Select every branch with xargs command. Delete branch with xargs git branch -D.

How do I delete all local branches except master?

Newer Git repositories have renamed the master branch to main. 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.

Can you delete local branches?

Deleting a branch LOCALLYDelete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.


1 Answers

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 init Initialized 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 \* | xargs Story-123-a Story-123-b Story-123-c $ git branch --merged | grep -v \* | xargs git branch -D Deleted branch Story-123-a (was 1d738b5). Deleted branch Story-123-b (was 1d738b5). Deleted branch Story-123-c (was 1d738b5). 
like image 52
GoZoner Avatar answered Oct 10 '22 00:10

GoZoner