Home Software Engineering How To Clean Up Git Branches

How To Clean Up Git Branches

by schkn

When working with Git, it is quite usual to accumulate many different branches for the different features we are working on.

However, when merged with our master branch, you may want to clean up unused branches in order for your Git workspace to be more organized.

As a developer, it can be quite tiring to have references to hundreds of different branches in our Git repository.

As a consequence, in this tutorial, we are going to see the different ways of cleaning up your Git branches easily.

This tutorial addresses cleaning up local, remote-tracking and remote branches.

Clean Up Local Git Branches

First of all, you want to check which branches have already been merged with your current branch.

In this case, we are going to imply that you want to delete local branches merged with master.

To check merged branches, use the “git branch” command with the “–merged” option.

$ git checkout master

$ git branch --merged <commit>

  feature
* master

If you omit to provide the commit hash, the command will imply that you are referring to HEAD (also known as the last commit of your current branch).

Now that you have the local branches already merged with master, you will need to delete them.

The easiest way to delete local Git branches is to use the “git branch” command with the “-d” option.

$ git branch -d <branch>

The “-d” option stands for “–delete” and it can be used whenever the branch you want to clean up is completely merged with your upstream branch.

If your branch is named “feature” for example, to clean up this branch, you would run

$ git branch -d release

Deleted branch feature (was bd6903f).

Force Delete Unmerged Git Branches

The other way of cleaning up local branches on Git is to use the “git branch” command with the “-D” option.

In this case, the “-D” option stands for “–delete -force” and it is used when your local branches are not merged yet with your remote tracking branches.

$ git branch -D <branch>

As you probably already know it, you have a local branch but you also have a remote-tracking which is a branch set to represent the state of your remote branch (also called the upstream branch).

As a consequence, if you perform a commit on your local branch without pushing it to the remote branch, your remote-tracking branch will be behind your local branch, thus unmerged.

To see differences between your local branch and your remote-tracking branch, execute the “git diff” command.

$ git diff <branch>..origin/<branch>

If there are any differences between the branches, you will have to use the “-D” option to delete the branch locally.

$ git branch -d <branch>

error: The branch 'branch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D branch'.

$ git branch -D <branch>
Deleted branch feature (was 022519a).

Now that your local branches are cleaned-up, let’s see how you can delete the remote tracking branches from your Git repository.

One-line command

In some cases, it might be useful to have a one-liner in order to delete local unused branches.

For those who are curious, here is how you can delete unused local branches in one single line.

$ git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d

Before executing this, let’s have a quick explanation about this command :

  • git branch –merged : first, you are simply listing all the branches currently merged with your current checked out branch;
  • egrep -v “(^*|master|dev)” : you are using the invert matching feature of grep in order to exclude any branches that may be called “master” or “dev”, just in case;
  • xargs git branch -d : you are deleting every single branch listed before.

Note : you can modify the egrep command in order to include your own branches.

Clean Up Remote Tracking Branches

As a reminder, a tracking-branch is a local branch set to track changes done on the remote branch of your Git server.

Those tracking branches are created in order to track changes but they may become obsolete if remote branches were deleted on the server.

In this case, let’s say that you have a local “feature” branch, a remote-tracking branch named “origin/feature”, but the “feature” branch has been deleted on the remote.

Git Remote Prune

In order to clean up remote tracking branches, meaning deleting references to non-existing remote branches, use the “git remote prune” command and specify the remote name.

$ git remote prune <remote>

In order to find the name of your current configured remotes, run the “git remote” command with the “-v” option.

$ git remote -v

origin  https://gitserver.com/user/repository.git (fetch)
origin  https://gitserver.com/user/repository.git (fetch)

In this example, the remote name is “origin”.

In order to delete remote tracking branches, we would then execute

$ git remote prune origin

Pruning origin
URL: https://gitserver.com/user/repository.git
 * [pruned] origin/feature

Prune while fetching

In some Git workflows, branches are deleted on the remote whenever they are integrated with the master branch.

Instead of having to prune your remotes periodically, you can also fetch the new references and prune your branches at the same time.

In order to clean up remote-tracking branches while fetching, use the “git fetch” command with the “–prune” option.

$ git fetch --prune <remote>

Alternatively, you can simply use the “-p” shortcut instead of typing “prune” every time.

$ git fetch -p <remote>

In the case of the origin remote, this would give

$ git fetch --prune origin

From https://gitserver.com/user/repository.git
 - [deleted]         (none)     -> origin/feature

However, specifying the prune option may be a bit tiring.

Luckily for you, you can configure your Git workspace in order to execute the prune operation every time you perform a fetch or a pull operation.

To configure Git to execute prune for every fetch, execute the following command

$ git config --global fetch.prune true

Defining your Gitflow workflow

When working with remote-tracking and local branches, it is important for you and your team to define a Git flow that you can stick to.

If you are working with somebody in order to review changes and approve them into your codebase, it might also be necessary for the reviewer to delete the branch on the remote afterwards.

This way, you will be able to prune your unused remote-tracking branches and your local branches as seen in the first section.

Defining this step is important as it can become quite messy to deal with dozens of different branches on your local Git repository.

Now that you have a clearer idea on how to delete unused remote-tracking branches, let’s see how you can perform the same trick for remote branches.

Clean Up Remote Branches

In our last chapter, we are going to see how we can delete remote branches when they are not used anymore.

Before performing any deletion operations on your Git repository, make sure that you are up-to-date with recent commits or merges done on your repository.

$ git pull

Now that you are up-to-date, you can starting deleting your old remote branches.

To clean up old remote branches, use the “git branch” command with the “-r” and “–merged” options.

As always, you need to be on the target branch to see branches already merged with this branch.

$ git checkout master

$ git branch -r --merged <commit>

origin/feature
origin/master

Note : if you don’t specify the commit, the command will simply imply that you are referring to HEAD (also known as the last commit on the branch)

Now that you know the remote branches already merged with master, you can use the “git push” command in order to delete remote branches.

$ git push <remote> --delete <branch>

In the example given previously, this command would give

$ git push origin --delete feature

To https://gitserver.com/user/repository.git
 - [deleted]         feature

One-line command

The command provided is actually very similar to the one used in order to delete local Git branches.

To delete unused remote branches, you can use the following one-liner

$ git branch -r --merged | egrep -v "(^\*|master|dev)" | xargs -n 1 git push --delete origin

Again, let’s have a quick explanation of the different parts of this command :

  • git branch -r –merged : in this case, you are listing remote branches that are currently merged with your current checked out branch. As a consequence, make sure that you are on the correct branch, remember that the “git branch –merged” command takes the HEAD when not provided with a commit SHA;
  • egrep -v “(^*|master|dev)” : in this part, you are using the invert-matching feature of grep in order to exclude the dev and the master branches;
  • xargs -n 1 git push –delete origin : in this case, you are taking every single branch listed and deleting it on the remote.

Conclusion

In this tutorial, you learnt all the ways of cleaning up unused branches on Git, whether they are local, remote tracking branches or remote branches.

If you are interested about Git and about software engineering, we have a complete section dedicated to it on the website, so make sure to check it out!

Delete unused git branches

You may also like

11 comments

Links 2311/2019: X.Org Server 1.20.6 and GNU Health 3.6.2 | Techrights November 23, 2019 - 7:28 pm

[…] How To Clean Up Git Branches […]

Reply
Max deWinter January 30, 2020 - 3:49 pm

Here’s a conclusion. GIT is out of control. You need a PhD in git to understand that lot.

Reply
Tiago Roldão June 10, 2020 - 7:36 am

I was throw for a moment here – the one line command won’t work, as it stands: it will try to delete “origin/my-branch” from origin, when it should really delete “my-branch” (the origin prefix makes no sense in the context)

An extra step would be necessary to clean it up:
git branch -r –merged | egrep -v “(^\*|master|dev)” | sed s/origin\\/// | xargs -I{} git push origin –delete {}

Reply
Arioch February 10, 2021 - 5:39 pm

$ git branch -d release

Deleted branch feature (was bd6903f).

…but…but… featrue or release?

Reply
Oksana March 6, 2021 - 5:06 pm

Thanks, very good article. Enjoyed it

Reply
Justin July 19, 2021 - 2:21 pm

This was a great roundup on cleaning up branches. Thanks for this!

Reply
Jordan August 26, 2021 - 7:52 pm

Thank you for leaving this! Very helpful

Reply
Foo November 19, 2021 - 8:26 am

A small remark: The one-line command would not delete a branch with the name “feature/P-90978_master_database” because it contains the word “master”. The regex should be “^(\*|master|dev)$” to just filter the word “master”.

Reply
Glen January 26, 2022 - 2:52 pm

This bit was very useful, thanks!

git config –global fetch.prune true

Reply
CoderX February 1, 2022 - 8:32 pm

This one worked for me
git branch -r –merged | egrep -v “(^\*|master|develop|release)” | sed s/origin\\/// | xargs -n 1 git push –delete origin

Reply
ali January 18, 2023 - 1:29 am

thanks! this gets rid of “origin\” from the beginning of grep command output otherwise git errors with missing ref

Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.