How to Delete All Local Branches in Git

Ankit Raj Goyal Feb 02, 2024
  1. Detailed Script to Delete All Local Branches in Git Except master
  2. Modify the Script to Delete Only the Merged Branches With the -d Option in Git
  3. Use an Easier Script to Delete All Local Branches in Git
  4. Modify the Script to Delete Local Branches With Names Similar to master Like master-prod in Git
  5. a Short Script to Delete All Local Branches in Git
  6. One-Line Hard Delete Command to Delete All Local Branches in Git
How 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.

  1. The for-each-ref iterates over all the references in the repo. The format string '%refname:short' extracts a short ref name from the iterables and the refs/heads points to their heads.

    We pass the output of for-each-ref to the grep command using the pipe | operator.

  2. The grep command searches over the reference heads passed to it to match against the expression string master\|main\|develop. This covers different names for your master branch - you may have named them main or develop.

  3. The -v option in the grep command is for inverted selection. Hence, it will only select those branches here than do not match the expression master\|main\|develop - essentially, it selects all local branches except the master.

  4. Finally, the xargs command takes all these selected branches and passes them as arguments to the command git branch -D. The git branch -D is the command to force-delete all the branches passed as arguments.

detailed script delete all local

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

merged non merged branches

We see first_branch is merged, but second_branch is not merged.

delete only merged branches

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.

smaller script delete all local

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

branch similar name master prod

delete branch similar name master prod

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.

short cut delete 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.

single line hard delete

Related Article - Git Branch