The way our team manage git branches is to create a new feature branch at the remote end, then we do a git pull and switch to the new feature branch locally. Next we do our development work, commit and push those changes. Finally we merge the reviewed code into a central remote branch. All pretty standard. When we merge, we tend to automatically delete the remote feature branch and then repeat the process. However, this can lead to having a lot of local branches that don’t track a remote branch anymore, and it’s good practice to tidy those up. So, how can you delete all the local branches that don’t track a remote (often searched for as “delete local branch git” or “git prune local branches“)? I also explain why
git prune remote origin is not the answer if you want to delete a local git branch!
Table of contents
List local and remote git branches
First of all have a look at all your local branches by typing:
… then take a look at all your remote branches by typing :
git branch -r
… or compare that to what Github is showing you exists at the remote end. You can do this with Github, Azure repos, whatever your remote git UI is. You’ll notice you have local branches that no longer exist in the remote repo – these are called ‘orphaned branches’, or branches that no longer track a remote, and it’s these that we want to delete.
List all orphaned git branches
This image shows the comparison between our local branches, shown in the Git bash window, and the remote branches, shown in the Github UI.
You can see that there are 3 remote branches but there are 6 remote branches – and we want to delete the 3 local branches which no longer track a remote branch. It’s often considered that
git prune is the answer, but it isn’t.
Git prune remote origin – what does it do?
It’s often thought that
git prune remote origin deletes all the local branches which no longer track a remote branch. Alas, no. This git command actually removes local references to remote branches. It DOES NOT remove local branches which no longer track a remote. This is often searched for as “git prune local branches” but there is no such command.
Git prune remote origin removes local references to remote branches. To understand what that means, read on.
Something that can be really confusing is that if you were to delete a remote branch in Github / Gitlab etc, and then run the
git branch -r command, you’ll still see the deleted branch in the list of remote branches. You *know* it’s been deleted and yet there it is.
The default options for
git fetch and
git pull do not ‘prune’ deleted remote branches by default, so to clear these confusing remote branch references up, type:
git remote prune origin
Summary – how to ‘delete orphaned local git branches’
The bad news is there is no single line command which deletes all local git branches that no longer track a remote, so you have two options to remove a local branch in git:
- Manually delete your local branches one by one. After comparing the remote and local branches you have a list of orphaned branches as shown above. And then delete the branch by typing
git branch -d thebranchnameand that will remove each.
- Find an automated command which does a bit of the hard work for you. I’m not going to list one here though because I haven’t found one that doesn’t carry a risk of deleting a branch incorrectly. I’d say look at this thread which discusses removing branches which no longer have a remote and then pick the solution which works best for you.