How to Delete All Local Branches in Git
-
Detailed Script to Delete All Local Branches in Git Except
master
-
Modify the Script to Delete Only the Merged Branches With the
-d
Option in Git - Use an Easier Script to Delete All Local Branches in Git
-
Modify the Script to Delete Local Branches With Names Similar to
master
Likemaster-prod
in Git - a Short Script to Delete All Local Branches in Git
- One-Line Hard Delete Command to Delete All Local Branches in Git
Old local branches clutter your local repository but provide no further value. You can delete all local branches in one go with the help of this tutorial.
We use commands such as grep
, xargs
, and for-each-ref
to write scripts for this task. We show various options for different use cases.
We also provide a one-line quick hack to delete all local branches at the end of the tutorial.
Detailed Script to Delete All Local Branches in Git Except master
We write the following script to Git delete all local branches except the master
branch.
git for-each-ref --format '%(refname:short)' refs/heads | grep -v "master\|main\|develop" |
xargs git branch -D
This script will delete all local branches except the master
branch, including the non-merged branches.
Let us break down this script to see how it works.
-
The
for-each-ref
iterates over all the references in the repo. Theformat
string'%refname:short'
extracts a short ref name from the iterables and therefs/heads
points to theirheads
.We pass the output of
for-each-ref
to thegrep
command using the pipe|
operator. -
The
grep
command searches over the reference heads passed to it to match against the expression stringmaster\|main\|develop
. This covers different names for yourmaster
branch - you may have named themmain
ordevelop
. -
The
-v
option in thegrep
command is for inverted selection. Hence, it will only select those branches here than do not match the expressionmaster\|main\|develop
- essentially, it selects all local branches except themaster
. -
Finally, the
xargs
command takes all these selected branches and passes them as arguments to the commandgit branch -D
. Thegit branch -D
is the command to force-delete all the branches passed as arguments.
Modify the Script to Delete Only the Merged Branches With the -d
Option in Git
We can modify the above script to only delete the merged branches. We replace the -D
option at the end with the -d
option.
The -d
is a soft delete option and only removes the merged branches.
git for-each-ref --format '%(refname:short)' refs/heads | grep -v "master\|main\|develop" |
xargs git branch -d
We see first_branch
is merged, but second_branch
is not merged.
Our modified script only deletes the merged first_branch
but leaves the non-merged second_branch
.
Use an Easier Script to Delete All Local Branches in Git
Some people may find the above script too complex and overkill. We can use an easier script to do the same task.
git branch | grep -v "master\|main\|develop" | xargs git branch -D
This script replaces the complex for-each-ref
with the simple git branch
command.
Please note that Git does not recommend using the git branch
in scripts. But we can deviate from the best practice sometimes for a quick solution.
Modify the Script to Delete Local Branches With Names Similar to master
Like master-prod
in Git
Sometimes, your repository may have branches with names similar to master
. For example, some of your branches could have names like master-prod
or master-draft
.
We can modify the regular expression in our script from "master\|main\|develop"
to "master$\|main$\|develop$"
to achieve this functionality.
git branch | grep -v "master$\|main$\|develop$" | xargs git branch -D
a Short Script to Delete All Local Branches in Git
We can pass clever regular expressions to grep
to shorten our script further.
git checkout master
git branch | grep -v "^*" |xargs git branch -D
The expression "^*"
means all branches starting (^
) with the current branch (*
). We did git checkout master
first, so our current branch is master
.
Thus -v "^*"
filters out our current branch, the master
and deletes all other local branches.
One-Line Hard Delete Command to Delete All Local Branches in Git
Finally, as promised, we will show you a single-line hard delete command to delete all local branches in Git.
git branch -D $(git branch)
This command deletes all branches except the one you are on.