Home

Delete All Local Git Branches Except One

Delete all local git branches except main, master, or your current branch.

Let’s take a look at a few different ways of deleting a batch of branches in a git repo.

Delete All Git Branches Using an Exclusion Pattern

You can list all git branches except a specific branch using the -v option in the grep command. For example, let’s say you wanted to list branches that do not include nodelete in the name.

git branch | grep -v nodelete

Now, to delete all branches that were returned, pipe to the xargs command and run git branch -d.

git branch | grep -v nodelete | xargs git branch -d
note

This command skips branches that are not fully merged, along with the current branch. It will output a message for every returned branch that it does not delete.

Delete All Git Branches Except main

For example, to skip the main branch, you could do this:

git branch | grep -v main | xargs git branch -d
note

This will skip any branch with main in its name, even if it’s something like new-domain.

Targeting the main Branch Exactly

The easiest way to target the main branch is to first check it out, then target the asterisk in the name:

git checkout main
git branch | grep -v "^* main$" | xargs git branch -d

Here, the ^ means "begins with" and $ means "ends with." Therefore, we’re matching exactly * main, which is how the main branch will be printed when it is checked out.

Getting Precise with the Branch List

If you don’t want to worry about checking out a branch, but want to be precise, you can choose to print the branches as a list with no whitespace. Keeping the example of main, here’s what you can do:

git branch --format='%(refname:short)' | grep -v "^main$" | xargs git branch -d

Force Deleting Git Branches

What I really like about using -d when deleting branches is that it skips branches that are not fully merged. If you want to delete all returned branches, you’ll want to use -D instead:

git branch | grep -v "..." | xargs git branch -D

Delete All Git Branches Except Current

Lastly, whether using the -d or -D option for deleting branches, you will not be able to delete the current branch. So you can technically just run through the entire list to delete everything except the current branch.

git branch | xargs git branch -d

Notice here that we don’t use the grep command because there’s nothing to filter from the response.

Let's Connect

Keep Reading

How To Delete a Git Tag

It's easy to delete a git tag from your local and remote repositories.

Dec 07, 2014

Git: Set Default Branch to "main" on "init"

Tired of remembering to rename the master branch after running git init? This option will help you!

Mar 18, 2021

Collaborate On A GitHub Gist

GitHub gists are designed to be user-specific and not for team collaboration. But you can make it work with a little finagling.

Apr 25, 2018