DEV Community

Tomasz Prasołek
Tomasz Prasołek

Posted on


How to clean up my branches?

You display the branch list and you don't know what is happening. Over 10 branches on the list. You read their names and you don't have a clue what it is. You wonder, "Is this my branches?" You go over it, you look at the last commits and you can see that it's yours. Some branches are two months old and they have a code from the tasks that has been in production for a long time.

Do you have such situations? In this post I will show you how to clean up in branches.

Local and remote branches

One of the more common types of working with branches in GIT is GitFlow or some variation thereof. In short, the point is that all new features and bug fixes we do on new branches. After finishing work, we merge (or rebase) these branches with our main development branch. Sometimes it's develop, sometimes master - it depends on the arrangements in the team.

As I mentioned above, if we do a little feature or fix a small bug, then usually we create a new branch for it. After finishing work - if we finished it in less than a working day - we merge our changes into the development branch and delete this local branch.

However, sometimes we have to leave the task for the next day. Then it's a good habit to push our branch to the server to have a copy of our work. The next day after finishing work, we must of course merge our work with the main branch, and then remove both the local branch and the remote branch.

Over time, the branch list may become larger if we do not delete them regularly.

However, how to check which branches have already been merged with the main one?

git branch --merged

This command will display a list of branches already merged, i.e. those that are to be deleted.

If we are curious about which branches we have not yet merged, then we will show them with the command:

git branch --no-merged

Potentially, such branches can also be removed if we know that it's some unnecessary branch on which, for example, we only tested some code.

Deleting the branches

Deleting a branch that has already been merged with the main branch:

git branch -d <branch_name>

If the branch has not been merged, information will be displayed on the console. To delete such branch you will need to use the command:

git branch -D <branch_name>

Above command is equivalent to:

git branch --delete --force <branch_name>

Deleting the remote branch from the server:

git push -d origin <branch_name>

git-curate tool

You can use the git-curate tool to clean up your local branches. It's a tool written in Ruby. After running, it displays each local branch together with information:

  • Date of last commit.
  • Last commit message.
  • Information whether it was merged into HEAD.
  • Information whether it has a remote branch.

At each branch, you can decide what to do with this branch. If you enter:

  • y - will mark the branch to be removed.
  • n - will mark branch, not to delete it.
  • done - will remove all branches previously marked for deletion and close the program.
  • abort - will close the program without any action.
  • help - will display help.

If we do larger clean up in branches, the git-curate tool will speed up this process.

Delete refs to remote branches

It also happens that several people work on the same branch. Sometimes we download someone's branch, because this person has a problem and asked us for help.

After finishing work, we delete local branches. And the person we helped removed the local branch and the one on the server.

Does it mean that there is no trace of this branch? Not completely. There is still a reference to the remote branch. This will be visible after entering the command:

git branch -a

The remote branch names will be preceded by remotes/origin/. For example, the remote branch for branch feature will have the name:


How do I delete references to remote branches? Just enter the command:

git fetch --prune

This command deletes all references to branches that no longer exist on the server.

Such references remain, because Git is not a typical online tool. If someone deletes branch on the server, which we once downloaded, it will not be automatically deleted in our local repository. Therefore, it is good to use the above command once in a while.


Remember to remove the local and remote branch immediately (if there is one). Two commands are enough for this:

Removal of the local branch:

git branch -d <branch_name>
git branch -D <branch_name> # enforcing deletion of the branch

Remote branch removal:

git push -d origin <branch_name>

And to delete references to already nonexistent branches, the command is:

git fetch - prune

I hope you are cleaning your branches regularly. If not, I'm counting on you to start it from now on.

I'm from Poland and english language is not my first language. This is my first post written in english, so If there are some mistakes in text - please just let me know :-)

Top comments (0)