Home » Git » Delete local branches in git that don’t track a remote

Delete local branches in git that don’t track a remote

By Emily

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!

List local and remote git branches

First of all have a look at all your local branches by typing:

git branch

… 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.

The Ultimate Managed Hosting Platform

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 delete local branches that don't track a remote branch

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.

prune branch references when the remote branch has been deleted

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
how to use git remote prune origin
You can see that after running git remote prune origin, 1 branch reference is removed. So that’s the branch references cleared up.

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:

  1. 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 thebranchname and that will remove each.
  2. 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.