TExcelTAD
Home › Git › Git Remote Advanced

Git Remote Advanced

Most projects have one remote called origin. But Git can talk to any number of remotes, and how fetch and push map branches between repositories is fully configurable. This lesson covers multiple remotes, refspecs, mirrors and bundles.

Several remotes

A remote is just a name for a URL plus some rules. Common setups: origin (your fork) + upstream (the original), or origin (GitHub) + backup (another server):

bash · try it yourself
git init -q --bare ~/github/app.git
git init -q --bare ~/gitlab/app.git
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "First version"
git remote add origin ~/github/app.git
git remote add backup ~/gitlab/app.git
git remote -v
git push -q origin main
git push -q backup main
git remote rename backup gitlab
git branch -r
backup	/home/learner/gitlab/app.git (fetch)
backup	/home/learner/gitlab/app.git (push)
origin	/home/learner/github/app.git (fetch)
origin	/home/learner/github/app.git (push)
  gitlab/main
  origin/main

Pushing to two servers at once

A remote can have several push URLs. git push origin then pushes to all of them, while fetching still uses the first URL:

bash · try it yourself
git init -q --bare ~/github/app.git
git init -q --bare ~/gitlab/app.git
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "First version"
git remote add origin ~/github/app.git
git remote set-url --add --push origin ~/github/app.git
git remote set-url --add --push origin ~/gitlab/app.git
git remote -v
git push origin main 2>&1 | grep -E '^To|main'
origin	/home/learner/github/app.git (fetch)
origin	/home/learner/github/app.git (push)
origin	/home/learner/gitlab/app.git (push)
To /home/learner/github/app.git
 * [new branch]      main -> main
To /home/learner/gitlab/app.git
 * [new branch]      main -> main

Refspecs: how branches are mapped

Every remote has a fetch refspec in .git/config. It says "copy the remote's refs/heads/* into my refs/remotes/origin/*". The + allows non-fast-forward updates of those tracking branches:

bash · try it yourself
git init -q --bare ~/github/app.git
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "First version"
git remote add origin ~/github/app.git
git push -q -u origin main
cat .git/config
[core]
	repositoryformatversion = 0
	filemode = true
	bare = false
	logallrefupdates = true
[remote "origin"]
	url = /home/learner/github/app.git
	fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
	remote = origin
	merge = refs/heads/main

You can write refspecs on the command line too: git fetch origin main:refs/remotes/origin/main, or git push origin HEAD:refs/heads/release (push the current commit to a branch called release). Fetching a single branch into a differently named local branch:

bash · try it yourself
git init -q --bare ~/github/app.git
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "First version"
git remote add origin ~/github/app.git && git push -q -u origin main
git push -q origin HEAD:refs/heads/release
git fetch -q origin release:local-release
git branch
git ls-remote --heads origin
  local-release
* main
3781a3ebdf63364f6413481de1f0c2451e605155	refs/heads/main
3781a3ebdf63364f6413481de1f0c2451e605155	refs/heads/release

Tracking information

git branch -vv shows which upstream each branch follows and how far ahead/behind it is. git branch -u changes it, and @{u} (upstream) works in any command:

bash · try it yourself
git init -q --bare ~/github/app.git
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "First version"
git remote add origin ~/github/app.git && git push -q -u origin main
git switch -q -c feature && git push -q origin feature
echo "wip" > wip.txt && git add . && git commit -q -m "Work in progress"
git branch -vv
git branch -u origin/feature
git branch -vv
git log --oneline @{u}..HEAD
* feature e9d5f90 Work in progress
  main    3781a3e [origin/main] First version
branch 'feature' set up to track 'origin/feature'.
* feature e9d5f90 [origin/feature: ahead 1] Work in progress
  main    3781a3e [origin/main] First version
e9d5f90 Work in progress

Mirrors and bundles

Tool Use
git clone --mirror <url> + git push --mirror <new-url> move a repository to another host with all refs (branches, tags, notes)
git bundle create repo.bundle --all pack a whole repository into one file, for transfer without network (USB stick, email)
git clone repo.bundle a bundle works like a read-only remote
bash · try it yourself
mkdir app && cd app && git init -q
echo "v1" > app.txt && git add . && git commit -q -m "First version"
git tag v1.0
echo "v2" > app.txt && git commit -q -am "Second version"
git bundle create -q ~/app.bundle --all
git bundle verify -q ~/app.bundle
git bundle list-heads ~/app.bundle
cd ~ && git clone -q app.bundle restored
git -C restored log --oneline --decorate
/home/learner/app.bundle is okay
777e4dda7ca800c889291684a71df8f2e144bcc6 refs/heads/main
3781a3ebdf63364f6413481de1f0c2451e605155 refs/tags/v1.0
777e4dda7ca800c889291684a71df8f2e144bcc6 HEAD
777e4dd (HEAD -> main, origin/main, origin/HEAD) Second version
3781a3e (tag: v1.0) First version

Housekeeping

Command Does
git remote show origin details: URLs, tracked branches, what push/pull do
git remote prune origin delete stale remote-tracking branches (same as fetch --prune)
git remote set-url origin git@github.com:ada/app.git switch HTTPS → SSH
git remote remove gitlab forget a remote and its tracking branches

Test yourself

What does the refspec +refs/heads/*:refs/remotes/origin/* mean?

  1. Push all local branches to origin
  2. On fetch, copy every remote branch to origin/<name>, allowing forced updates
  3. Delete remote branches
  4. Fetch only tags

What does git push origin HEAD:refs/heads/release do?

  1. Creates a local branch release
  2. Pushes the current commit to a branch named release on origin
  3. Renames the remote
  4. Fetches release

Which tool packs a repository into a single file you can clone from?

  1. git archive
  2. git bundle
  3. git stash
  4. git gc