Working with Several Remotes

Updated · 10 min read
hardGit 2.53gitupstreamforkmirrorrefspecinterview

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:

Terminal
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 -v
Output
origin	/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:

Terminal
git remote add upstream ../../school-fee-tracker.git
git remote -v
Output
origin	/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)
RemotePoints toNavika uses it to
originHer forkPush her branches, open pull requests from them
upstreamThe original projectFetch the newest official work

Keep a Fork in Sync

Advitha adds the late fee to the official project:

Terminal
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 upstream
Output
From ../../school-fee-tracker
 * [new branch]      main       -> upstream/main

The 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:

Terminal
git merge --ff-only upstream/main
git push origin main
Output
Updating 8d08fe1..f5cc2ae
Fast-forward
 fees.txt | 1 +
 1 file changed, 1 insertion(+)
To /home/navika/projects/navika/../navika-fee-tracker.git
   8d08fe1..f5cc2ae  main -> main
Terminal
git log --oneline --all
Output
f5cc2ae (HEAD -> main, upstream/main, upstream/HEAD, origin/main, origin/HEAD) Add the late fee
8d08fe1 Add README and the term fee

main, 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.

Two remotes: your fork and the original

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:

Terminal
git remote set-url --push upstream no-push
git remote -v
Output
origin	/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:

Terminal
git push upstream main
Output
fatal: '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:

Terminal
git fetch --all
Output
Fetching origin
Fetching upstream

git 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:

Terminal
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.git
Output
To ../new-host-fee-tracker.git
 * [new branch]      main -> main

The 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

CommandIt is used to
git remote add upstream <url>Add the original project as a second remote
git fetch upstreamFetch from one named remote
git fetch --allFetch from every remote
git merge --ff-only upstream/mainBring 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:

Terminal
cd ../navika/fee-tracker
git config --get-regexp "^remote\.upstream\."
Output
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
Terminal
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
Output
* main 756342c [origin/main] Add the hostel fee

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

Quick check: Several remotes

4 questions
1In a fork workflow, which remote do you push your branches to?
2How do you bring the original project's new commits into your fork?
3What does git remote set-url --push upstream no-push do?
4What does git push --mirror do?

Key Takeaways

Quick reference

CommandIt is used to
git remote add upstream <url>Add the original project
git fetch upstreamFetch the official work
git merge --ff-only upstream/mainUpdate your main from the original
git push origin mainUpdate your fork
git fetch --allFetch every remote
git remote set-url --push upstream no-pushBlock 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?

Lesson 42 of 106