Git Remote Advanced
Git Remote Advanced: Managing Multiple Remotes and Refspecs
Once you go beyond a single origin remote, Git lets you synchronize across forks, mirror repositories to secondary hosts, clean up stale tracking branches, and customize how local and remote branches map to each other.
Inspecting Tracking Branches with git branch -vv
Run git branch -vv to view every local branch, its linked remote-tracking branch, and how many commits ahead or behind it sits:
git branch -vv
If a local branch shows [origin/feature/header-nav: gone], that branch was already deleted on GitHub (for instance after a PR merged), so you can safely delete the local copy with git branch -d feature/header-nav.
Pruning Stale Remote-Tracking References
When teammates delete merged branches on GitHub, your local origin/* references stay behind until pruned. Fetch updates and delete stale remote pointers together:
git fetch --prune
To make pruning automatic on every git fetch or git pull, enable it globally once:
git config --global fetch.prune true
Pushing to Multiple Mirrors and Custom Refspecs
You can push a local branch to a differently named remote branch using source:destination refspec syntax, or attach multiple push URLs to one remote so one git push backs up to both GitHub and GitLab:
git push origin feature/header-nav:preview/header-v2
git remote set-url --add --push origin git@gitlab.com:your-username/starter-site.git
git remote -v
Checking Remote Health Without Cloning
Use git ls-remote <url> to list every branch and tag hash on a remote server before cloning or debugging SSH/token connectivity:
git ls-remote origin
Common Advanced Remote Mistakes
Adding a second --push URL replaces the original implicit push URL unless you also re-add the first URL with --add --push. Always check git remote -v right after modifying push URLs so commits still go to origin.
Frequently Asked Questions
Does git fetch --prune delete any of my local branches?
No. --prune only removes stale read-only origin/* tracking pointers whose branches no longer exist on the remote server. Your local branches remain untouched.
What does git push origin :old-branch mean?
Pushing an empty source (:old-branch) is legacy refspec syntax for deleting old-branch on origin, equivalent to git push origin --delete old-branch.
How do I fetch a Pull Request ref directly without adding the contributor's fork?
GitHub exposes read-only PR refs under refs/pull/<id>/head. Fetch one locally with git fetch origin pull/42/head:pr-42 and inspect it with git switch pr-42.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With