Skip to content

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 โ€‹

TermMeaning
RemoteNamed reference to a hosted repository (e.g. origin)
Tracking branchLocal branch linked to a remote branch (e.g. main tracking origin/main)
FetchDownload objects + refs (no working tree change)
PullFetch + integrate (merge or rebase)
PushUpload local commits to a remote branch

Listing Remotes โ€‹

bash
git remote -v

Output 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.git

Changing URLs โ€‹

bash
git remote set-url origin git@github.com:example/app.git

Removing a Remote โ€‹

bash
git remote remove upstream

Fetching Updates โ€‹

bash
git fetch
git fetch --all          # All remotes
git fetch origin main    # Specific branch

Viewing Remote Branches โ€‹

bash
git branch -r            # Remote-only
git branch -a            # Local + remote

Creating a Tracking Branch โ€‹

bash
git checkout -b feature/ui origin/feature/ui
# or
git switch -c feature/ui --track origin/feature/ui

Setting Upstream After Creation โ€‹

bash
git branch --set-upstream-to=origin/main main
# or first push
git push -u origin main

Pull Strategies โ€‹

Default pull merges. Many teams prefer rebase:

bash
git config --global pull.rebase true

Or per repository:

bash
git config pull.rebase true

Safe Update Pattern โ€‹

bash
git fetch origin
git rebase origin/main   # or merge if policy requires

Pushing Branches โ€‹

bash
git push origin feature/auth

Delete remote branch:

bash
git push origin --delete feature/auth

Renaming 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-name

Pruning Stale Remote References โ€‹

bash
git remote prune origin
git fetch --prune

Inspect Remote Details โ€‹

bash
git remote show origin

Displays 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 main

Mirroring (Administrative) โ€‹

bash
git clone --mirror https://github.com/source/repo.git
cd repo.git
git push --mirror git@internal:backup/repo.git

Authentication Tips โ€‹

  • Prefer SSH for stability
  • Use personal access tokens for HTTPS when 2FA enabled
  • Cache credentials: git config --global credential.helper cache

Common Issues โ€‹

IssueCauseFix
Rejected push (non-fast-forward)Remote has new commitsgit pull --rebase then push
Authentication failedInvalid token / keyRegenerate credentials
Detached HEAD editingChecked out remote ref directlyCreate a branch: git switch -c fix upstream/main
Stale tracking branchesRemote branch deletedgit fetch --prune

Best Practices โ€‹

  1. Use consistent remote names (origin, upstream)
  2. Enable pruning to reduce clutter
  3. Avoid force-pushing shared branches
  4. Keep main protected (branch protection rules)
  5. 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