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”)? 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 branches
First of all have a look at all your local branches by typing:
List remote branches
… 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.
Git prune remote origin – what does it do?
You might think that
git prune remote origin will delete all of these local branches – but alas, no. It actually removes local references to remote branches. It DOES NOT clean up local branches which no longer track a remote.
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 and then pick the solution which works best for you.