Working with Remote Repositories โ
Introduction โ
Remotes let you collaborate, back up work, and deploy code. This tutorial covers adding, inspecting, syncing, pruning, and managing remote branches safely.
Key Concepts โ
| Term | Meaning |
|---|---|
| Remote | Named reference to a hosted repository (e.g. origin) |
| Tracking branch | Local branch linked to a remote branch (e.g. main tracking origin/main) |
| Fetch | Download objects + refs (no working tree change) |
| Pull | Fetch + integrate (merge or rebase) |
| Push | Upload local commits to a remote branch |
Listing Remotes โ
bash
git remote -vOutput shows fetch/push URLs.
Adding a Remote โ
bash
git remote add origin https://github.com/example/app.git
git remote add upstream https://github.com/org/app.gitChanging URLs โ
bash
git remote set-url origin git@github.com:example/app.gitRemoving a Remote โ
bash
git remote remove upstreamFetching Updates โ
bash
git fetch
git fetch --all # All remotes
git fetch origin main # Specific branchViewing Remote Branches โ
bash
git branch -r # Remote-only
git branch -a # Local + remoteCreating a Tracking Branch โ
bash
git checkout -b feature/ui origin/feature/ui
# or
git switch -c feature/ui --track origin/feature/uiSetting Upstream After Creation โ
bash
git branch --set-upstream-to=origin/main main
# or first push
git push -u origin mainPull Strategies โ
Default pull merges. Many teams prefer rebase:
bash
git config --global pull.rebase trueOr per repository:
bash
git config pull.rebase trueSafe Update Pattern โ
bash
git fetch origin
git rebase origin/main # or merge if policy requiresPushing Branches โ
bash
git push origin feature/authDelete remote branch:
bash
git push origin --delete feature/authRenaming a Local Branch (and Remote) โ
bash
git branch -m old-name new-name
git push origin :old-name new-name
git push origin -u new-namePruning Stale Remote References โ
bash
git remote prune origin
git fetch --pruneInspect Remote Details โ
bash
git remote show originDisplays tracking, stale branches, and push/pull configuration.
Multiple Remotes Workflow โ
Fork model example:
bash
git remote add upstream https://github.com/original/project.git
git fetch upstream
git rebase upstream/main # Keep fork updated
git push origin mainMirroring (Administrative) โ
bash
git clone --mirror https://github.com/source/repo.git
cd repo.git
git push --mirror git@internal:backup/repo.gitAuthentication Tips โ
- Prefer SSH for stability
- Use personal access tokens for HTTPS when 2FA enabled
- Cache credentials:
git config --global credential.helper cache
Common Issues โ
| Issue | Cause | Fix |
|---|---|---|
| Rejected push (non-fast-forward) | Remote has new commits | git pull --rebase then push |
| Authentication failed | Invalid token / key | Regenerate credentials |
| Detached HEAD editing | Checked out remote ref directly | Create a branch: git switch -c fix upstream/main |
| Stale tracking branches | Remote branch deleted | git fetch --prune |
Best Practices โ
- Use consistent remote names (
origin,upstream) - Enable pruning to reduce clutter
- Avoid force-pushing shared branches
- Keep
mainprotected (branch protection rules) - Rotate tokens/SSH keys periodically
Summary โ
Mastering remotes enables efficient collaboration. Fetch intentionally, pull with awareness of strategy, prune regularly, and keep upstream hygiene strong.
Next Steps โ
- Code review workflows (
pull-requests-and-code-review-workflow.md) - Conflict handling (
git-conflict-resolution-strategies.md)
Key Commands
bash
git remote -v
git fetch --all --prune
git push origin <branch>
git remote show origin
git remote prune origin