How can I clean up deleted branches?

After working on a project for a while, you will accumulate local branches that track remote branches which have already been deleted. Feature branches get merged, teammates clean up after themselves on the remote, but your local repository still holds references to all of them. Here is how to tidy things up.

Prune stale remote-tracking branches

When a branch is deleted from the remote, your local copy does not automatically know about it. Running git fetch --prune tells Git to remove any remote-tracking references that no longer exist on the remote:

$ git fetch --prune
 - [deleted]         (none)     -> origin/feature/old-login
 - [deleted]         (none)     -> origin/fix/typo-readme

This only removes the remote-tracking references (like origin/feature/old-login). It does not touch your local branches.

Delete local branches that are already merged

After pruning, you probably still have local branches sitting around for work that has long been merged. You can list and delete them in one go:

$ git branch --merged main | grep -v "main" | xargs git branch -d
Deleted branch feature/old-login (was 3a1f2b4).
Deleted branch fix/typo-readme (was 9c8d7e6).

Replace main with whatever your primary branch is called. The -d flag only deletes branches that have been fully merged, so there is no risk of losing unmerged work.

Auto-prune on every fetch

If you want pruning to happen automatically every time you fetch or pull, set it once:

$ git config --global fetch.prune true

From now on, git fetch and git pull will always clean up stale remote-tracking references without you having to remember the --prune flag.

One-liner to clean everything

Combine pruning and merged-branch deletion into a single command:

$ git fetch --prune && git branch --merged main | grep -v "main" | xargs git branch -d

Run this periodically (or alias it) and your local repository stays clean.

See what is left

After cleaning up, check the state of your remaining branches:

$ git branch --verbose
* main             a1b2c3d Latest release
  feature/new-api  d4e5f6a [origin/feature/new-api] WIP: new endpoints

The --verbose flag shows each branch alongside its tracking relationship and latest commit, so you can quickly spot anything that still needs attention. For more details, see the git fetch and git branch command references. You can also read about deleting branches and managing remotes.


DeployHQ deploys directly from your Git branches, so keeping them organised matters. Try it free.