I want to delete all branches that get listed in the output of ...
$ git branch
... but keeping current branch, in one step. Is th
I use this because I get to be more selective in what I do not want to delete. This below command removes every branch except master, develop and the current branch.
BRANCHES=$(git branch | egrep -v "(master|develop|\*)" | xargs git branch -D)
echo $BRANCHES
So I put this in my ~/.zshrc
delete_branches() {
BRANCHES=$(git branch | egrep -v "(master|develop|\*)" | xargs git branch -D)
echo $BRANCHES
}
alias cleanup_branches=delete_branches
So I see a lot of hard coded branch names here... And I think my answer here is more accurate to the "current branch" part of the question whilst keeping it single line and readable to bash newbies like me. Just to place credit where it's due, the answer is rather obviously also based on @pankijs's answer.
git branch | grep -v $(git branch --show-current) | xargs git branch -d
and I have it aliased on one line in my .bash_aliases in debian aswell.
alias gitbclean='git branch | grep -v $(git branch --show-current) | xargs git branch -d'
(Though I thinks some bash features need to be enabled for the sub command to run on some command lines)
IMHO, the safest way of removing local branches is:
git branch -av | grep "\[gone\]" | awk '{print $1}' | xargs git branch -d
Also, more info related to this topic you can find Delete all local git branches
To delete all branches except for the current branch in one step:
git branch | grep -v $(git rev-parse --abbrev-ref HEAD) | xargs git branch -D
Based on @pankijs answer, I made two git aliases:
[alias]
# Delete all local branches but master and the current one, only if they are fully merged with master.
br-delete-useless = "!f(){\
git branch | grep -v "master" | grep -v ^* | xargs git branch -d;\
}; f"
# Delete all local branches but master and the current one.
br-delete-useless-force = "!f(){\
git branch | grep -v "master" | grep -v ^* | xargs git branch -D;\
}; f"
To be added in ~/.gitconfig
And, as @torek pointed out:
Note that lowercase
-d
won't delete a "non fully merged" branch (see the documentation). Using-D
will delete such branches, even if this causes commits to become "lost"; use this with great care, as this deletes the branch reflogs as well, so that the usual "recover from accidental deletion" stuff does not work either.
Basically, never use the -force
version if you're not 300% sure you won't lose anything important. Because it's lost forever.
git branch -d
(or -D
) allows multiple branch names, but it's a bit tricky to automatically supply "all local branches excluding the one I'm on now" without writing at least a little bit of code.
The "best" (formally correct) method is to use git for-each-ref
to get the branch names:
git for-each-ref --format '%(refname:short)' refs/heads
but then it's even harder to figure out which branch you're on (git symbolic-ref HEAD
is the "formally correct" method for this, if you want to write a fancy script).
More conveniently, you can use git branch
, which prints your local branch names preceded by two spaces or (for the current branch) by an asterisk *
. So, run this through something to remove the *
version and you're left with space-separated branch names, which you can then pass to git branch -d
:
git branch -d $(git branch | grep -v '^*')
or:
git branch | grep -v '^*' | xargs git branch -d
Note that lowercase -d
won't delete a "non fully merged" branch (see the documentation). Using -D
will delete such branches, even if this causes commits to become "lost"; use this with great care, as this deletes the branch reflogs as well, so that the usual "recover from accidental deletion" stuff does not work either.