Tidying Up: Pruning Remote-Tracking Branches and Deleting Merged and Gone Branches
Keep branch lists short and meaningful: pruning stale remote-tracking branches, finding local branches whose upstream is gone, deleting merged branches safely, recognizing squash-merged branches, Git 2.56's --delete-merged and --forked, cleaning up remote branches, and a routine for it all.
kvi5hnuCode Sprout team member7 min readOn this page
After a few months of feature branches and pull requests, git branch -a lists dozens of branches, most long finished. They don't slow Git down, but they make it hard to see what's active, and old branches get checked out or pushed by mistake. This article covers finding and deleting finished branches, locally and on the remote, without losing anything unmerged.
There are three kinds of leftover:
- Stale remote-tracking branches:
origin/feature/xremains after the branch was deleted on the server. - Local branches whose upstream is gone: your
feature/x, which tracked a branch that was merged and deleted on the server. - Old branches on the remote that nobody deleted after merging.
Stale remote-tracking branches: prune
Remote-tracking branches are only updated when you fetch, and by default a fetch doesn't remove ones whose branch was deleted on the server. git fetch --prune does:
git fetch --prune # or -p
git remote prune origin # the same, without fetching anything new
git remote prune origin --dry-run
git config --global fetch.prune true # prune on every fetch from now on
Pruning only removes origin/... references. It never touches your local branches. Article 19 shows it in action.
Local branches whose upstream is gone
After pruning, a local branch whose upstream was deleted shows gone in git branch -vv. That usually means its pull request was merged and the branch deleted on the server:
git init -q --bare server.git
git clone -q server.git sam 2>/dev/null; cd sam
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"; git push -q -u origin main
for b in lake ridge forest; do
git switch -q -c feature/$b main
echo "$b" > $b.txt; git add .; git commit -q -m "Add $b page"; git push -q -u origin feature/$b
done
git switch -q main
cd ..; git clone -q server.git maintainer; cd maintainer # on the server: lake merged, ridge squash-merged
git merge -q --no-ff origin/feature/lake -m "Merge pull request: lake"
git merge -q --squash origin/feature/ridge > /dev/null 2>&1; git commit -q -m "Add ridge page (#2)"
git push -q origin main; git push -q origin --delete feature/lake feature/ridge
cd ../sam
git fetch -q --prune
git pull -q --ff-only
echo "=== git branch -vv"
git branch -vv
echo "=== just the names of gone branches"
git for-each-ref --format='%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)' refs/heads | grep .
echo "=== delete them with -d: refuses anything unmerged"
git branch -d feature/lake feature/ridge
=== git branch -vv
feature/forest 3841065 [origin/feature/forest] Add forest page
feature/lake 483f142 [origin/feature/lake: gone] Add lake page
feature/ridge e02e068 [origin/feature/ridge: gone] Add ridge page
* main 8896e75 [origin/main] Add ridge page (#2)
=== just the names of gone branches
feature/lake
feature/ridge
=== delete them with -d: refuses anything unmerged
error: the branch 'feature/ridge' is not fully merged
hint: If you are sure you want to delete it, run 'git branch -D feature/ridge'
hint: Disable this message with "git config set advice.forceDeleteBranch false"
Deleted branch feature/lake (was 483f142).
feature/lake was merged with a merge commit, so -d deleted it. feature/ridge was squash-merged: main contains a new commit with the same changes, not the branch's commit, so Git can't tell it was merged, and -d refuses. That's the safe outcome. Check before forcing:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c feature/ridge
echo "Ridge loop: 8 km" > ridge.txt; git add .; git commit -q -m "Add ridge page"
git switch -q main
git merge -q --squash feature/ridge > /dev/null; git commit -q -m "Add ridge page (#2)"
echo "=== git cherry: - means an equivalent change is already in main"
git cherry -v main feature/ridge
echo "=== does the branch have anything main lacks? (empty = no)"
git diff main feature/ridge -- $(git diff --name-only main...feature/ridge)
git branch -D feature/ridge
=== git cherry: - means an equivalent change is already in main
- 2883589f7aac43a57825d06a43252bb8bc8b44cc Add ridge page
=== does the branch have anything main lacks? (empty = no)
Deleted branch feature/ridge (was 2883589).
- For a branch whose commits were each cherry-picked or rebase-merged,
git cherry -v main <branch>marks each with-when an equivalent change is inmain(article 16). - For a squash of several commits, no single commit matches. Compare the files the branch changed: if
git diff main <branch> -- <those files>is empty,mainhas everything the branch changed. That's what the second command does. - Then delete with
-D.
The common one-liner for gone branches, which uses -d and so only deletes what Git can prove is merged:
git fetch --prune && git for-each-ref --format='%(if:equals=[gone])%(upstream:track)%(then)%(refname:short)%(end)' refs/heads | grep . | xargs -r git branch -d
It's safer than parsing git branch -vv with awk, because for-each-ref output is meant for scripts, and safer than -D, which would also delete gone branches with unpushed work.
Merged branches
Regardless of upstreams, git branch --merged main lists local branches whose tip is contained in main. They can all be deleted with -d:
git switch main && git pull --ff-only
git branch --merged main | grep -v -E '^\*|^\+|^ *(main|develop)$' | xargs -r git branch -d
The grep keeps the current branch (*), branches checked out in other worktrees (+), and long-lived branches. --no-merged main lists the opposite: branches with work not in main, which deserve a look before deleting.
Git 2.56: --delete-merged and --forked
Git 2.56 added two options for branches that track a shared branch, such as topic branches created with git switch -c fix --track origin/main:
git branch --forked <pattern>lists branches whose upstream matches the pattern.git branch --delete-merged <pattern>deletes branches whose upstream matches the pattern and that are fully merged into it.--dry-runshows what it would delete. It skips the current branch, and branches named the same as their upstream, such asmaintrackingorigin/main.
git init -q --bare server.git
git clone -q server.git sam 2>/dev/null; cd sam
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"; git push -q -u origin main
git switch -q -c fix-typo --track origin/main # topic branches tracking origin/main
sed -i 's/guide/Guide/' README.md; git commit -q -am "Fix title capitalization"
git switch -q -c add-maps --track origin/main
echo "maps" > maps.txt; git add .; git commit -q -m "Add maps"
git switch -q main
git merge -q fix-typo; git push -q # fix-typo is now in origin/main
echo "=== branches forked from origin/main"
git branch --forked origin/main
echo "=== dry run, then delete"
git branch --delete-merged origin/main --dry-run
git branch --delete-merged origin/main
git branch
=== branches forked from origin/main
add-maps
fix-typo
* main
=== dry run, then delete
Would delete branch fix-typo (was 9778704).
Deleted branch fix-typo (was 9778704).
add-maps
* main
fix-typo was merged into origin/main, so it was deleted. add-maps has unmerged work, and main was skipped as it has the same name as its upstream. For branches that track their own remote branch, such as feature/x tracking origin/feature/x, use the gone-branch approach above instead.
Cleaning up branches on the remote
Hosting services can delete a branch automatically when its pull request is merged (a repository setting on GitHub, and a merge request option on GitLab). Turn it on, and most remote clutter never appears.
For branches left behind, list remote branches merged into main, review the list, and delete them:
git fetch --prune
git branch -r --merged origin/main | grep -v -E 'origin/(HEAD|main|develop)$'
git push origin --delete feature/old-thing feature/older-thing
To find abandoned branches, sort by the date of their last commit:
git for-each-ref --sort=committerdate refs/remotes/origin \
--format='%(committerdate:short) %(authorname) %(refname:short)'
Ask the authors before deleting branches with unmerged work. Deleted branches can be recovered from a clone that still has them, and sometimes through the hosting service, such as GitHub's Restore branch button on a merged or closed pull request.
A cleanup routine
Run this every week or two, or make it an alias (article 34):
git switch main
git pull --ff-only
git fetch --prune
git branch --merged main | grep -v -E '^\*|^\+|^ *(main|develop)$' | xargs -r git branch -d
git branch -vv | grep ': gone]' # look at what's left, check, delete with -D
Everything it deletes automatically is provably merged. Whatever remains is either active work, or squash-merged branches to check by hand.
What's current
fetch.prune=trueis a common global setting.- Git 2.56's
git branch --delete-mergedand--forkedhandle topic branches that track a shared upstream. - Hosting services offer automatic deletion of merged pull request branches, and many teams enable it on every repository.
- Squash and rebase merges, popular on hosting services, are why "gone but not merged" branches are common. Checking with
git cherryor a diff before-Dis the safe habit.
Common questions
Does git fetch --prune delete my local branches?
No. It only removes remote-tracking branches, such as origin/feature/x, whose branch no longer exists on the server. Your local branches stay, marked gone if they tracked one.
Why won't git branch -d delete a branch whose pull request was merged?
The pull request was probably squash-merged or rebase-merged, so main has new commits with the same changes, not the branch's commits. Check with git cherry -v main <branch> or a diff, then use -D.
Is it safe to delete merged branches?
Yes. A merged branch's commits are all in main, so deleting the name loses nothing. -d checks this for you.
I deleted a branch by mistake. How do I get it back?
git branch -d/-D prints the branch's last commit ID. Recreate it with git branch <name> <id>, or find the ID in git reflog (article 26).
Summary
git fetch --prune, orfetch.prune=true, removes stale remote-tracking branches.git branch -vvmarks local branches whose upstream was deleted asgone.for-each-refwith%(upstream:track)lists them for scripts.- Delete with
-d, which refuses unmerged branches. Squash-merged branches need checking withgit cherry -vor a diff, then-D. git branch --merged mainlists branches safe to delete.- Git 2.56's
--delete-merged <pattern>deletes branches merged into their matching upstream, and--forkedlists them. - On the remote, enable automatic branch deletion after merging, and clean up with
git branch -r --mergedandgit push origin --delete.
Practice questions
git branch -vv shows feature/maps 3a4b5c6 [origin/feature/maps: gone] Add maps. What does it mean, and is it safe to delete?
Answer
The branch's upstream, origin/feature/maps, was deleted on the server and pruned locally, typically because its pull request was merged. Try git branch -d feature/maps: if it succeeds, the work is in your current branch. If it refuses, the branch was likely squash-merged, or has commits that were never merged. Check with git cherry -v main feature/maps and git log main..feature/maps before deleting with -D.
Write a command that deletes every local branch already merged into main, except main and develop, without risking unmerged work.
Answer
git switch main && git pull --ff-only
git branch --merged main | grep -v -E '^\*|^\+|^ *(main|develop)$' | xargs -r git branch -d
--merged lists only branches contained in main, and -d would refuse anything unmerged anyway.
What's the difference between git fetch --prune and git branch --delete-merged origin/main?
Answer
git fetch --prune removes remote-tracking branches (origin/...) whose branch no longer exists on the server, and never touches local branches. git branch --delete-merged origin/main (Git 2.56) deletes local branches whose upstream is origin/main and that are fully merged into it.
Review quiz
Test yourself on Part 4 with the Git Part 4 review quiz.
Previous: Git Workflows: Feature Branches, Pull Requests, GitHub Flow, Gitflow, Forking and Trunk-Based
Next: Undoing Changes: restore, reset, revert and Detached HEAD

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.