596

I'd like to clean up my local repository, which has a ton of old branches: for example 3.2, 3.2.1, 3.2.2, etc.

I was hoping for a sneaky way to remove a lot of them at once. Since they mostly follow a dot release convention, I thought maybe there was a shortcut to say:

git branch -D 3.2.*

and kill all 3.2.x branches.

I tried that command and it, of course, didn't work.

3
  • 43
    git branch -D $(git branch | grep 3.2*) - this worked for me. It deletes the branches whose name starts with "3.2". grep - pattern matching in the output (of git branch in this case). $() - means execute and place the result. | - chaining.
    – Eduard
    Commented Aug 5, 2018 at 9:37
  • 10
    Worth noting for those that don't know, that -D is a force delete, should use -d in most cases to be safer first.
    – redfox05
    Commented Feb 6, 2019 at 17:25
  • This blog contains a short answer medium.com/@rajsek/… Just git branch | grep "<pattern>" | xargs git branch -D much easier Commented Jan 25, 2022 at 17:37

37 Answers 37

1
2
0

I just cleaned up a large set of obsolete local/remote branches today.

Below is how I did it:

1. list all branch names to a text file:

git branch -a >> BranchNames.txt

2. append the branches I want to delete to command "git branch -D -r":

git branch -D -r origin/dirA/branch01 origin/dirB/branch02 origin/dirC/branch03 (... append whatever branch names)

3. execute the cmd

And this one works so much faster and reliable than "git push origin --delete".

This may not be the most smart way as listed in other answers, but this one is pretty straight forward, and easy to use.

0

I was able to delete many of my branches using the below shell script:

#!/bin/bash
# Get a list of remote branches matching the regex pattern
matching_branches=($(git ls-remote --heads | grep -E "/3.*" | awk -F'/' '{print $3}'))

# Loop through the matching branches and delete each one remotely
for branch in "${matching_branches[@]}"
do
  # Delete remotely
  git push --delete origin "$branch"
done

By running this I am able to delete multiple branches with some similar in the branch names. This is the output:

sidharth@Sidharths-Air test-repo % bash delete-branch.sh                                                  
From https://github.com/sidharthvijayakumar/test-repo.git
To https://github.com/sidharthvijayakumar/test-repo.git
 - [deleted]         3.0
To https://github.com/sidharthvijayakumar/test-repo.git
 - [deleted]         3.1
sidharth@Sidharths-Air test-repo % 
0

So the easiest way would be to : git branch | grep -v "master" | xargs git branch -D

this will remove all the branches except for the master branch and current branch

-1

As in Git all the branches are nothing by references to the git repo, why don't you just delete the branches going to .git/ref and then if anything is left out which is not interesting in the repository will automatically be garbage collected so you don't need to bother.

-1

If you are using Fish shell, you can leverage the string functions:

git branch -d (git branch -l "<your pattern>" | string trim)

This is not much different from the Powershell options in some of the other answers.

-4

You can remove all the branches removing all the unnecessary refs:

rm .git/refs/heads/3.2.*

2
  • 3
    Downvoted for two reasons: 1. it uses an implementation detail (the fact that current versions of Git store the branches as files in a certain directory). This can change in future versions rendering the answer invalid. 2. It doesn't work if the repository is located in a separate directory.
    – axiac
    Commented Oct 12, 2017 at 9:17
  • Also, this produces very unexpected results if you run this on a packed repository (git gc). Don't do that. Commented Feb 5, 2018 at 13:14
-4

git branch -D <branchName>

1
2

Not the answer you're looking for? Browse other questions tagged or ask your own question.