Working with Several Remotes
Your contact list usually has more than one entry for the people that matter: a work number and a home number, or a friend's old number and their new one. Each has its own name, and you pick the right one for each call.
A repository can have several remotes in the same way. The most common case is a fork: you push to your own copy of a project (origin), and you fetch the newest work from the original project (upstream). Other cases are a backup server, a second hosting platform, or moving a whole repository from one host to another. Every command you know (fetch, pull, push) takes a remote name, so working with several is mostly a matter of naming the right one.
In this lesson, you will set up the fork workflow with origin and upstream, keep the fork up to date, protect yourself from pushing to the wrong remote, and copy a repository completely to a new host.
The Starting Point: A Fork
The school's official repository is school-fee-tracker.git, maintained by Advitha. Navika can't push to it, so she made her own copy on the server, a fork, navika-fee-tracker.git, and cloned that. On GitHub, GitLab and Bitbucket, the "Fork" button makes such a copy. Here, git clone --bare plays that button:
git init --bare --quiet school-fee-tracker.git
mkdir advitha navika
cd advitha
git init --quiet fee-tracker
cd fee-tracker
git config user.name "Advitha Anamaneni"
git config user.email "advitha@example.com"
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git remote add origin ../../school-fee-tracker.git
git push --quiet -u origin main
cd ../..
git clone --bare --quiet school-fee-tracker.git navika-fee-tracker.git
cd navika
git clone --quiet ../navika-fee-tracker.git fee-tracker
cd fee-tracker
git remote -vorigin /home/navika/projects/navika/../navika-fee-tracker.git (fetch)
origin /home/navika/projects/navika/../navika-fee-tracker.git (push)origin is Navika's fork. Now she adds the original project as a second remote. By convention, it is called upstream:
git remote add upstream ../../school-fee-tracker.git
git remote -vorigin /home/navika/projects/navika/../navika-fee-tracker.git (fetch)
origin /home/navika/projects/navika/../navika-fee-tracker.git (push)
upstream ../../school-fee-tracker.git (fetch)
upstream ../../school-fee-tracker.git (push)| Remote | Points to | Navika uses it to |
|---|---|---|
origin | Her fork | Push her branches, open pull requests from them |
upstream | The original project | Fetch the newest official work |
Keep a Fork in Sync
Advitha adds the late fee to the official project:
cd ../../advitha/fee-tracker
echo 'Late fee: $250' >> fees.txt
git commit --quiet -am "Add the late fee"
git push --quiet
cd ../../navika/fee-tracker
git fetch upstreamFrom ../../school-fee-tracker
* [new branch] main -> upstream/mainThe official main is now upstream/main in Navika's repository, next to origin/main. Her fork doesn't have the new commit yet. To bring her fork up to date, she merges upstream/main into her main, and pushes it to origin:
git merge --ff-only upstream/main
git push origin mainUpdating 8d08fe1..f5cc2ae
Fast-forward
fees.txt | 1 +
1 file changed, 1 insertion(+)
To /home/navika/projects/navika/../navika-fee-tracker.git
8d08fe1..f5cc2ae main -> maingit log --oneline --allf5cc2ae (HEAD -> main, upstream/main, upstream/HEAD, origin/main, origin/HEAD) Add the late fee
8d08fe1 Add README and the term feemain, origin/main and upstream/main are at the same commit: the fork is in sync. Run these three steps (fetch upstream, merge, push origin) before you start each new piece of work. GitHub's "Sync fork" button does the same on the server.
Never Push to upstream by Accident
Even if the server would refuse it, typing git push upstream by mistake is easy. A remote can have a separate push URL. Setting it to something that doesn't exist makes pushing to that remote impossible, while fetching still works:
git remote set-url --push upstream no-push
git remote -vorigin /home/navika/projects/navika/../navika-fee-tracker.git (fetch)
origin /home/navika/projects/navika/../navika-fee-tracker.git (push)
upstream ../../school-fee-tracker.git (fetch)
upstream no-push (push)The fetch URL is unchanged, and the push URL is now no-push. A push fails at once:
git push upstream mainfatal: 'no-push' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.The other use of a separate push URL is the opposite: one remote that pushes to two places, for example a hosting platform and a backup server. git remote set-url --add --push origin <url> adds push URLs, and every git push origin then goes to all of them.
Fetch from Every Remote
git fetch --all fetches every remote in one command:
git fetch --allFetching origin
Fetching upstreamgit remote update does the same. Neither changes your branches.
Move a Repository to a New Host: Mirrors
When a project moves from one hosting platform to another, you want everything: every branch, every tag, and nothing renamed. A mirror clone copies all references exactly, and a mirror push sends them all:
cd ../..
git init --bare --quiet new-host-fee-tracker.git
git clone --mirror --quiet school-fee-tracker.git moving.git
cd moving.git
git push --mirror ../new-host-fee-tracker.gitTo ../new-host-fee-tracker.git
* [new branch] main -> mainThe project has only one branch, main, and it arrived on the new host. With more branches and tags, every one of them would be listed here. --mirror also deletes branches on the target that don't exist in the source, so use it only on a new, empty repository, or one you mean to replace completely.
Syntax
| Command | It is used to |
|---|---|
git remote add upstream <url> | Add the original project as a second remote |
git fetch upstream | Fetch from one named remote |
git fetch --all | Fetch from every remote |
git merge --ff-only upstream/main | Bring the official work into your main |
git remote set-url --push <remote> <url> | Give a remote a different push URL |
git remote set-url --add --push <remote> <url> | Add another push URL, so one push goes to several places |
git clone --mirror <url> | Copy every reference of a repository |
git push --mirror <url> | Make the target exactly match the local repository |
Common Mistakes
Pushing feature branches to upstream. Push to origin, your fork, and open a pull request from there.
Syncing the fork by pulling origin. origin is your fork, which only has what you pushed. Fetch from upstream.
Committing on main in a fork. Your main then diverges from upstream/main, and syncing needs merges. Keep main as a clean copy of upstream/main, and work on feature branches.
Using git push --mirror on a repository that has other work. It deletes every branch the source doesn't have. Mirror only into an empty or disposable repository.
Calling the original project origin. It works, but every guide and tool expects origin to be yours and upstream the original. Stick to the convention.
Under the Hood
Each remote has a fetch refspec, the rule that says which server branches go where. git remote add creates the standard one:
cd ../navika/fee-tracker
git config --get-regexp "^remote\.upstream\."remote.upstream.url ../../school-fee-tracker.git
remote.upstream.fetch +refs/heads/*:refs/remotes/upstream/*
remote.upstream.pushurl no-push+refs/heads/*:refs/remotes/upstream/* reads: take every branch (refs/heads/*) on the server and store it as refs/remotes/upstream/<name>, and allow non-fast-forward updates (+). That is why the same branch name can exist as origin/main and upstream/main without any clash: they are stored in different folders. remote.upstream.pushurl is the push URL you set.
You can change a refspec. For example, git config remote.upstream.fetch "+refs/heads/main:refs/remotes/upstream/main" fetches only main from a huge project. And a mirror clone's refspec is +refs/*:refs/*: everything, stored under the same names.
Interview Corner
What is the difference between origin and upstream?
They are naming conventions. origin is the remote you cloned from, usually your own fork, where you push. upstream is the original project you forked from, which you fetch from to stay up to date and contribute to through pull requests.
How do you keep a fork up to date with the original repository?
Add the original as upstream once. Then git fetch upstream, git switch main, git merge --ff-only upstream/main (or git rebase upstream/main), and git push origin main.
How do you move a repository, with all branches and tags, to a new host?
git clone --mirror <old-url>, then git push --mirror <new-url> into an empty repository on the new host. Afterwards, update origin in each clone with git remote set-url.
What is a refspec?
A rule of the form [+]<source>:<destination> that maps references between repositories. A fetch refspec like +refs/heads/*:refs/remotes/origin/* stores the server's branches as remote-tracking branches. A push refspec like main:main says which local branch updates which remote branch.
How can one git push update two servers?
Add a second push URL to the remote with git remote set-url --add --push origin <url> (after adding the original URL as a push URL too). Every push to origin then goes to all push URLs.
Practice
Challenge: sync again. Advitha adds the hostel fee to the official project. In Navika's clone, sync her fork: fetch upstream, fast-forward main, and push to origin. Then show git branch -vv.
What does Git print? Before you open the solution, decide which remote main's upstream is in git branch -vv, and whether it says ahead or behind.
Show solution
cd ../../advitha/fee-tracker
echo 'Hostel fee: $9,000' >> fees.txt
git commit --quiet -am "Add the hostel fee"
git push --quiet
cd ../../navika/fee-tracker
git fetch --quiet upstream
git merge --quiet --ff-only upstream/main
git push --quiet origin main
git branch -vv* main 756342c [origin/main] Add the hostel feemain still has origin/main, her fork, as its upstream, from the clone, and it is up to date with it, because she just pushed. git branch -vv doesn't mention upstream/main at all: the remote called upstream and a branch's upstream are different things that share a name. With git branch -u upstream/main, as in the tip above, the brackets would show upstream/main instead.
Checkpoint. Fork a public repository on GitHub, GitLab or Bitbucket, clone your fork, and add the original as upstream. Block pushes to it with set-url --push, then practice syncing: fetch upstream, merge into main, push to origin.
Key Takeaways
Quick reference
| Command | It is used to |
|---|---|
git remote add upstream <url> | Add the original project |
git fetch upstream | Fetch the official work |
git merge --ff-only upstream/main | Update your main from the original |
git push origin main | Update your fork |
git fetch --all | Fetch every remote |
git remote set-url --push upstream no-push | Block pushes to upstream |
git clone --mirror <url> | Copy a repository completely |
git push --mirror <url> | Replace the target with a complete copy |
Was this tutorial helpful?