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):
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 -rbackup /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:
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:
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:
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:
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 |
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?
- Push all local branches to origin
- On fetch, copy every remote branch to origin/<name>, allowing forced updates
- Delete remote branches
- Fetch only tags
What does git push origin HEAD:refs/heads/release do?
- Creates a local branch release
- Pushes the current commit to a branch named release on origin
- Renames the remote
- Fetches release
Which tool packs a repository into a single file you can clone from?
- git archive
- git bundle
- git stash
- git gc