Sub topic 13
Deleting remote branches and pruning stale references
This chapter assumes you can create and switch branches, and that you know how git fetch and git push work. It covers two cleanup jobs: removing a branch from the remote, and removing the local references that still point to branches that no longer exist there.
Three kinds of branch, again
A feature branch can exist in three places at once:
- The local branch, such as
feature/login. - The branch on the remote, stored on the server.
- The remote-tracking branch, such as
origin/feature/login. This is your local copy of what you last saw on the remote.
Deleting one does not delete the others. Cleanup means handling each of the three.
Deleting a branch on the remote
Use git push with --delete:
git push origin --delete feature/loginGit asks the remote to remove the branch feature/login. On success, Git also removes your local remote-tracking branch origin/feature/login. Your local branch feature/login stays. Delete it separately:
git branch -d feature/loginThe -d option refuses to delete a branch that has unmerged commits. Use -D only when you are sure you want to discard them.
Why stale references appear
Suppose a teammate deletes feature/login on the remote, or the hosting service deletes it automatically after a pull request is merged. Your repository is not told. Your remote-tracking branch origin/feature/login stays in place, because git fetch only adds and updates references by default. These leftovers are called stale references.
They cause confusion. git branch -r keeps listing branches that are gone, and tab completion suggests them.
Pruning stale references
Pruning removes remote-tracking branches whose remote branch no longer exists:
git fetch --prune
git branch -vvgit fetch --prune contacts the remote, updates everything as usual, and then deletes local remote-tracking branches with no matching branch on the remote. The output reports each removal:
- [deleted] (none) -> origin/feature/logingit branch -vv lists each local branch with its upstream and its state. If a local branch's upstream was pruned, Git marks it as gone:
feature/login 3f2a9c1 [origin/feature/login: gone] Add login form
* main a81b0e7 [origin/main] Update readmeA gone branch is a good candidate for git branch -d. Pruning never touches local branches, only remote-tracking ones.
You can also prune without fetching:
git remote prune originTo prune on every fetch, set a configuration option:
git config --global fetch.prune trueThe cleanup workflow
The steps below show the whole sequence after a pull request is merged.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `remote ref does not exist` | The branch is already deleted on the remote | Run `git fetch --prune` to clear the stale reference |
| `not fully merged` on `git branch -d` | The branch has commits not in your current branch | Merge it first, or use `-D` if you want to discard it |
| Deleted branch reappears | A teammate with an old local copy pushed it again | Ask them to prune and delete their local copy |
| Delete rejected by the server | The branch is protected | Change the protection settings on the hosting service |
Exercises
Exercise
You run git push origin --delete feature/login. Which of these still exist afterward: the local branch feature/login, and the remote-tracking branch origin/feature/login?
Show answer
The local branch feature/login still exists. The remote-tracking branch origin/feature/login is removed as part of the successful push. You delete the local branch yourself with git branch -d feature/login.
Exercise
After git fetch --prune, git branch -vv shows [origin/fix-typo: gone] beside a local branch. What happened, and what should you check before deleting that branch?
Show answer
The fix-typo branch was deleted on the remote, and pruning removed origin/fix-typo, so the local branch's upstream is gone. Before running git branch -d fix-typo, confirm its commits were merged. -d will refuse if they were not.