How to delete all the Git, BitBucket branches which have been merged?


I usually work with multiple projects at once and whenever there is a new feature on which I had to work, I usually create a new Branch from my Master branch and once I am done with the new feature I do create a pull request and after the approval, I do merge my changes from my new Branch to my Master branch.

Well, this workflow of creating new Branch from Master is pretty widely used across the developer community for the Continuous Integration and Continuous delivery but there are some side effects with this workflow.

Assume you have a team of 20 developers working on the same project and each developer is working with his own branch, so you can easily see the count of branches has reached 20 along with 1 Master and if none of the developers is bothered about deleting the branch after merging his branch to master then the branches will keep on piling up. Soon within a span of a couple of months, you will have hundreds of inactive branches.

In this post, we will see all the different ways to delete merged branches which are no longer actively used along with that we will also look on how to make exceptions for not deleting some of the branches.


Here are the steps for deleting all the merged branches?

  1. List all the branches which have been merged
  2. Delete a branch which is merged locally
  3. Delete all remote branches which have been merged remotely
  4. Prune the deleted branch to get rid of remote tracking
  5. Git Alias to delete all the merged git branches


1. List all the branches which have been merged

The first step which I would take is to list out all the branches which have been merged.

Here is the git command to list out all the merged branches -

1git branch --list -a --merged

Well, the above command will return you a really long list of branches if you have so many branches. I would recommend putting some filters in the command so that you can filter the branches which you have created.

1git branch --list -a --merged | egrep "my_branch_name"  

Here is the breakdown of the command -

  1. git branch --list -a --merged - This will list out all the merged branches
  2. egrep "my_branch_name" - This will filter only the branch which has the name my_branch_name


2. Delete a branch that is merged locally

In the Step-1 we have seen how you can list or filter the branches. Now if your branch exists locally then you can use the following command along with the command which we prepared in the Step-1.

So the idea is simple "First filter the branches which you want to delete and then delete the branches"

Here is the command for deleting the branch locally -

1git branch --list -a --merged | egrep "my_branch_name" | xargs git branch -d

(*Note- The above command will only delete the branch which exists locally not on remote. To delete the remote branch please refer to Step-3)

Here is the breakdown of the command -

  1. git branch --list -a --merged - This will list out all the merged branches
  2. egrep "my_branch_name" - This will filter only the branch which has the name my_branch_name
  3. xargs git branch -d - It will delete all the branches which are filtered


3. Delete all remote branches which have been merged remotely

Now we have seen how to list and filter the merged branches in the Step-1 and now in this step, we are going to delete all the remote branches.

(*Note - A quick note all the remote branches always starts with remotes/origin/, if it does not start with remotes/origin/ then its a local branch)

So here is the git command for delete -

1git branch --list -a --merged | egrep "my_branch_name" | cut -d "/" -f 3- | xargs git push --delete origin 

Here is the breakdown of the command -

  1. git branch --list -a --merged - This will list out all the merged branches

  2. egrep "my_branch_name" - This will filter only the branch which has the name my_branch_name. But since we are targeting the remote branch to so your remote branches should look like this -

    • remotes/origin/feature/my-test-branch-1
    • remotes/origin/feature/my-test-branch-2
    • remotes/origin/feature/my-test-branch-3
  3. cut -d "/" -f 3- - It will remove/trim the remotes/origin/ from the branch names

  4. xargs git push --delete origin - This will delete all the remote branches



4. Prune the deleted branch to get rid of remote tracking

This is an additional step for pruning which needs to be performed strictly after Step 3. With pruning, you can get rid of remote-tracking branches.

Here is the command for pruning the remote branches -

1git remote prune origin 


5. Git Alias to delete all the merged git branches

If you have gone through the previous steps- Step-1,Step-2,Step-3 then you can easily see that you have to type in the delete command each time when you want to get rid of merged branches.

But there is a cool feature of Git know as Git Alias which can help you to create some useful easy-to-remember alias names so that you do not have to write down long commands for deleting the merged branches.


5.1 Here is an alias for deleting the local merged branches -

1 git config --global alias.delete-local-merged-branches '!git branch --list -a --merged | egrep "rwagh-CARD-15740" | xargs git branch -d'

Once you created the alias you can simply run the following git command

1git delete-local-merged-branches 

5.2 Here is an alias for deleting the remote merged branches -

1git config --global alias.delete-remote-merged-branches '!git branch --list -a --merged | egrep "rwagh-CARD-15740" | cut -d "/" -f 3- | xargs git push --delete origin'

Once you created the alias you can simply run the following git command and it will delete all the merged remote branches

1git delete-remote-merged-branches 

References

  1. StackOverflow - How can I delete all Git branches which have been merged