Months of tasks leave months of branches. Some are merged and safe to drop. Some were deleted on the remote and left a ghost here. A few still hold work that exists nowhere else. Sort them before you delete.
Branches checked out in other folders are a separate job: see how to clean up old worktrees first.
See which branches lost their remote
Fetch and drop the remote-tracking refs the remote no longer has. In this example the repository is ~/code/acme/web and the remote is origin.
git remote prune --dry-run origin
Pruning origin
URL: git.example.com:acme/web.git
* [would prune] origin/checkout-fix
* [would prune] origin/old-spike
These two branches were deleted on the remote, but your copy of the list still has them. The dry run changes nothing. When the list looks right, fetch with pruning.
git fetch --prune origin
From git.example.com:acme/web
- [deleted] (none) -> origin/checkout-fix
- [deleted] (none) -> origin/old-spike
Your local branches are untouched. Now ask Git which of them tracked a branch that is gone.
git branch -vv
checkout-fix 487798e [origin/checkout-fix: gone] Fix checkout total
hotfix dfcb25c Fix tax rounding
* main 3f188c1 [origin/main] Merge hotfix
old-spike 524ffbc [origin/old-spike: gone] Try a new cart
gone means the remote branch no longer exists. It does not mean the branch is merged: a teammate may have deleted a branch nobody merged. hotfix has no bracket because it never had an upstream.
Check which branches are merged
git branch --merged main
checkout-fix
hotfix
* main
The tip of each listed branch is in the history of main, including hotfix, which was never pushed. The * marks your current branch. To check just the gone ones, run this.
git for-each-ref --format='%(refname:short) %(upstream:track)' refs/heads |
sed -n 's/ \[gone\]$//p' |
while read b; do
git merge-base --is-ancestor "$b" main && echo "merged $b" || echo "NOT merged $b"
done
merged checkout-fix
NOT merged old-spike
Delete the merged ones
Use lowercase -d. It refuses a branch it can’t prove is merged.
git branch -d checkout-fix hotfix
Deleted branch checkout-fix (was 487798e).
Deleted branch hotfix (was dfcb25c).
The commit IDs are your way back: git branch checkout-fix 487798e recreates a branch you deleted by mistake, for as long as Git keeps the commit.
Look twice at the rest
git branch -d old-spike
error: The branch 'old-spike' is not fully merged.
If you are sure you want to delete it, run 'git branch -D old-spike'.
Git is telling you that old-spike has commits main doesn’t. Read them first.
git log --oneline main..old-spike
524ffbc Try a new cart
Keep the branch if you still want that commit. If you’re done with it, git branch -D old-spike deletes it. Note the commit ID before you do.
A squash merge also shows up here. Its branch tip never lands in main, so -d refuses even though the work shipped. Confirm the pull request included the branch’s latest commit before you use -D.
Delete a remote branch
Deleting on the remote affects your team. See the safe remote deletion check first.
Check for unpushed work first
A branch with no upstream, like hotfix above, can hold commits that were never pushed. Find unpushed commits before you delete anything you can’t see on the remote.
push& (Pushand) keeps a count of the commits waiting to be pushed across your repositories, so you can see which branches still hold unpushed commits before you delete them.