Delete all local branches in Git
Old local branches clutter your local repository but provide no further value. You can delete all local branches at once with the help of this tutorial.
We script this task using commands such as grep
, , xargs
and for-each-ref
. We show various options for different use cases.
We also provide a one-line quick hack for deleting all local branches at the end of the tutorial.
Verbose script to delete all local branches in Git exceptmaster
We write the following script to Git to delete master
all local branches except the branch.
git for-each-ref --format '%(refname:short)' refs/heads | grep -v "master\|main\|develop" |
xargs git branch -D
This script will delete master
all local branches except the branch, including unmerged branches.
Let's break down this script and see how it works.
-
for-each-ref
Iterate over all references in the repo.format
string'%refname:short'
Extract a short ref name from the iterable andrefs/heads
point to themheads
.We use a pipe to pass the output
|
offor-each-ref
togrep
the command operator. -
grep
The command searches the ref heads passed to it for matches to the expression stringmaster\|main\|develop
. This coversmaster
different names for your branches - you might have named themmain
ordevelop
. -
grep
The -p option in the command-v
is used to invert the selection. So it will only select thosemaster\|main\|develop
branches that do not match the expression - essentially, it selectsmaster
all local branches except . -
Finally,
xargs
the command gets all these selected branches and passes them as arguments to the commandgit branch -D
.git branch -D
is the command that forcefully deletes all branches passed as arguments.
-d
Modify the script to delete only merged branches
using the option in Git
We can modify the above script to delete only the merged branches. We -d
replace the final -D
option with the option.
-d
is a soft delete option that only deletes merged branches.
git for-each-ref --format '%(refname:short)' refs/heads | grep -v "master\|main\|develop" |
xargs git branch -d
We see first_branch
that is merged, but second_branch
is not merged.
Our modified script removes only the merged ones first_branch
, but leaves the unmerged ones second_branch
.
Deleting all local branches in Git using a simpler script
Some people may find the above script overly complex and overkill. We can use a simpler script to accomplish the same task.
git branch | grep -v "master\|main\|develop" | xargs git branch -D
git branch
This script replaces complex commands
with simple ones for-each-ref
.
Note that Git is not recommended for use in scripts git branch
. But sometimes we can deviate from best practices to get a quick solution.
Modify the script to delete master
local branches with names likemaster-prod
Sometimes, your repository might have master
branches with names like . For example, some of your branches might have names like master-prod
or .master-draft
"master\|main\|develop"
We can modify
the regular expression in the script from to "master$\|main$\|develop$"
to achieve this function.
git branch | grep -v "master$\|main$\|develop$" | xargs git branch -D
Short script to delete all local branches in Git
We can pass some clever regular expressions to grep
to shorten our script even further.
git checkout master
git branch | grep -v "^*" |xargs git branch -D
The expression represents all branches starting "^*"
from the current branch ( ) . We did first , so our current branch is .*
^
git checkout master
master
Thus -v "^*"
filtering out our current branch master
and deleting all other local branches.
Single line hard delete command to delete all local branches in Git
Finally, as promised, we'll show you a one-line hard delete command for deleting all local branches in Git.
git branch -D $(git branch)
This command deletes all branches except the one you are on.
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Changing drives in Git Bash
Publish Date:2025/03/30 Views:56 Category:Git
-
This short article will discuss how we can use Git Bash to have a Unix-style command line environment in Windows operating system and run multiple commands in this terminal. Git Bash Git is a collection of command-line utilities created to
Adding a remote branch in Git
Publish Date:2025/03/30 Views:142 Category:Git
-
Git does not allow its developers to create new branches on remote repositories. But instead, we can push an already existing local branch, and after doing so, we can bring it to the remote repository using some Git commands. In every versi
Synchronize your local repository with a remote repository in Git
Publish Date:2025/03/30 Views:92 Category:Git
-
This article outlines the process of syncing your local repository with a remote repository. We will also see how to sync a GitHub branch with a remote repository on the command line. Synchronize your local repository with a remote reposito
Creating a remote repository from a local repository in Git
Publish Date:2025/03/30 Views:105 Category:Git
-
This article discusses the necessary steps to create a remote repository based on a local repository. This is ideal when you have a local repository that needs to be available on a remote or SSH-enabled server. Creating a remote repository
Removing the upstream repository in Git
Publish Date:2025/03/30 Views:177 Category:Git
-
This article will teach you how to delete an upstream repository in Git. We may sometimes need to delete or change the remote repository that we use with our local repository. To do this, we can use the Git command git remote . Removing the
Git remote add SSH
Publish Date:2025/03/30 Views:53 Category:Git
-
In this day and age, the most widely used version control system is Git, which is operated by most developers within a team structure. This is mainly used to increase code efficiency, no matter how big or critical the project is. In this se
Differences between Git Reset, Revert and Checkout commands
Publish Date:2025/03/30 Views:185 Category:Git
-
This article discusses the differences between the git reset , , git revert and git checkout commands. These are some of the most useful Git utilities that allow us to undo certain changes in our repository. It’s easy to get confused with
Git revert local commits
Publish Date:2025/03/30 Views:84 Category:Git
-
When a software engineer or a web developer uses Git, it is obvious that he pushes a lot of code and commits to the Git repository every day, and while doing so, the need to undo or revert a particular commit or a set of commits arises from
Deleting local and remote Git branches
Publish Date:2025/03/30 Views:146 Category:Git
-
Git comes into play in version control systems when you are working with a team and the entire team is making changes to the same code repository. Creating a new branch in Git is relatively easy than other version control systems and deleti