Topic 3.5
Keeping Remotes Tidy: Pruning, Renaming Branches & Multiple Remotes
In one line
Remote-tracking branches like origin/feature/coupons stay on your laptop even after the branch is deleted on GitHub, until you prune them (git fetch --prune, or fetch.prune true). git branch -vv shows local branches whose upstream is gone. Renaming a branch means renaming it locally, pushing the new name, and deleting the old one on the remote. git remote add/rename/remove/set-url manage the remotes themselves.
Think of it like this
A phone's contact list. When a friend changes number or a shop closes, your phone doesn't know until you sync and clean up. Old entries pile up and you call the wrong number.
Words you'll meet
New words in this topic, in plain English. Come back here whenever one feels fuzzy.
- Stale remote-tracking branch
- A local
origin/…ref for a branch that was already deleted on the remote. - Prune
- Remove stale remote-tracking branches during or after a fetch.
- fetch.prune
- A setting that makes every fetch prune automatically.
- gone
- What
git branch -vvshows when a local branch's upstream no longer exists. - git branch -m
- Rename (move) a branch.
- origin/HEAD
- A pointer to the remote's default branch, set when you clone.
Step by step
01Old branches piling up
Six months in, Priya's git branch -a lists dozens of origin/… branches whose PRs were merged and deleted long ago. She prunes them in one fetch.
02Local branches whose upstream is gone
Pruning cleans origin/… refs, but her own local branches stay. -vv shows which ones lost their upstream, meaning they were merged and deleted on GitHub. She deletes those with -d, which still refuses if a branch has unmerged work.
03Renaming a branch everywhere
Arjun named a branch feature/rateings. Renaming it takes three steps: local rename, push the new name with tracking, delete the old name on GitHub.
04The repository moved
Tiffin moves from a personal account to the tiffin-app organisation, and renames the default branch from master to main on GitHub. Each developer updates their clone once.
Break it on purpose
Errors are the best teachers. Make each change, read the error, guess what went wrong, then reveal the answer.
Break #1
Pushing to a branch that was deleted
Priya's PR was merged and its branch deleted. Next week she commits more on the same local branch and runs git push.
Myth vs fact
Myth
Deleting a branch on GitHub removes it from everyone's laptop.
Fact
Each clone keeps its own copies until it prunes, and local branches stay until their owner deletes them.
Pro corner
Extra depth for experienced readers. New to this? Skip it for now and come back later.
- ▸
A one-liner to delete every local branch whose upstream is gone:
git fetch -p && git branch -vv | awk '/: gone]/{print $1}' | xargs -r git branch -d. Use-d, not-D, so anything unmerged is kept.
Remember this
- 1
Deleting a branch on GitHub (for example after merging a PR) doesn't delete your
origin/…copy.git fetch --pruneremoves remote-tracking branches that no longer exist on the remote. - 2
Turn it on once:
git config --global fetch.prune true, and every fetch and pull cleans up. - 3
Your local branches are never pruned automatically.
git branch -vvmarks those whose upstream disappeared with: gone], so you can delete them withgit branch -d. - 4
Rename a branch:
git branch -m old newlocally,git push -u origin new, thengit push origin --delete old. Open PRs from the old name must be re-opened or retargeted. - 5
Remotes are just names for URLs:
git remote -v,add,rename,remove,set-url. You can have many, such asorigin(your fork) andupstream(Topic 3.3). - 6
origin/HEADrecords the remote's default branch. If the default changes (master → main), update it withgit remote set-head origin -a.
Explain it without notes
Why do origin/… branches remain after they're deleted on GitHub?
How do you rename a branch that's already on GitHub?
Practice
Prune stale branches and delete local ones whose upstream is gone.
Trade-offs
- ↔
Auto-pruning keeps branch lists accurate but removes the last local record of a deleted remote branch (the reflog still has it). Renaming shared branches is cheap for Git but costs coordination with everyone using them.
Done when you can
I prune remote-tracking branches.
I clean up local branches marked gone.
I can rename branches and update remotes.