Git: removing unnecessary branches

When working with Git, a large list of unnecessary branches can grow over time on your development computer. This is because performing a pull-request removes the remote branch but it leaves local one intact. However, there is a relatively easy way to list branches that no longer have their counterparts on the server.

First, we run the command:

  git fetch -p
 

Git will then update the state of the local branches to what is currently on the server. Now we execute another command:

git branch -vv

We will then get a list of all branches with additional information about them, for example:

develop 123456789abc [origin/develop: behind 64] Commit 1
feature/some-feature 193001835021 [origin/feature/some-feature] Commit 2
feature/some-other-feature 2840fc0239fc [origin/feature/some-other-feature: gone] Commit 3

The local branches are usually associated with specific branches on the server: those are listed in the square brackets. However, if the name of the remote branch is followed by “gone”, it has already been deleted. In most cases, a local branch that no longer has its remote counterpart can be deleted freely.