Command Palette

Search for a command to run...

Hectal
PHASE 3Intermediate ~10 min· topic 5 of 5

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.

0/5 · 0%

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 -vv shows 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.

terminal
$ git branch -r | wc -l
git fetch --prune
git branch -r | wc -l
git config --global fetch.prune true
── expected output ──
47
From github.com:tiffin-app/tiffin
- [deleted] (none) -> origin/feature/coupons
- [deleted] (none) -> origin/fix/payment-timeout
- [deleted] (none) -> origin/feature/dispatch
...
6

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.

terminal
$ git branch -vv
git branch -d feature/coupons fix/payment-timeout
── expected output ──
feature/coupons 6a1b2c3 [origin/feature/coupons: gone] Validate coupon expiry
fix/payment-timeout d3e4f5a [origin/fix/payment-timeout: gone] Retry Razorpay timeout
* main 0e9d8c7 [origin/main] Merge branch 'feature/coupons'
wip/eta 9b8a7c6 Rough ETA idea
Deleted branch feature/coupons (was 6a1b2c3).
Deleted branch fix/payment-timeout (was d3e4f5a).
If the PR was squash-merged, `-d` may refuse, because the original commits aren't on main (Topic 4.5). Check the PR was merged, then use `-D`.

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.

terminal
$ git branch -m feature/rateings feature/ratings
git push -u origin feature/ratings
git push origin --delete feature/rateings
── expected output ──
* [new branch] feature/ratings -> feature/ratings
branch 'feature/ratings' set up to track 'origin/feature/ratings'.
To github.com:tiffin-app/tiffin.git
- [deleted] feature/rateings
Renaming a branch everywherediagram
Rendering diagram…

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.

terminal
$ git remote set-url origin git@github.com:tiffin-app/tiffin.git
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a
── expected output ──
branch 'main' set up to track 'origin/main'.
origin/HEAD set to main

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.

terminal
$ git push
── what you'll see ──
To github.com:tiffin-app/tiffin.git
* [new branch] feature/coupons -> feature/coupons
The branch is recreated on GitHub with old, already-merged commits plus the new one, and it's confusing to open a PR from.

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. 1

    Deleting a branch on GitHub (for example after merging a PR) doesn't delete your origin/… copy. git fetch --prune removes remote-tracking branches that no longer exist on the remote.

  2. 2

    Turn it on once: git config --global fetch.prune true, and every fetch and pull cleans up.

  3. 3

    Your local branches are never pruned automatically. git branch -vv marks those whose upstream disappeared with : gone], so you can delete them with git branch -d.

  4. 4

    Rename a branch: git branch -m old new locally, git push -u origin new, then git push origin --delete old. Open PRs from the old name must be re-opened or retargeted.

  5. 5

    Remotes are just names for URLs: git remote -v, add, rename, remove, set-url. You can have many, such as origin (your fork) and upstream (Topic 3.3).

  6. 6

    origin/HEAD records the remote's default branch. If the default changes (master → main), update it with git remote set-head origin -a.

Explain it without notes

01

Why do origin/… branches remain after they're deleted on GitHub?

02

How do you rename a branch that's already on GitHub?

Practice

01

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.