Sub topic 14
Adding a Second Remote Such as upstream
This chapter assumes you can already push a branch to origin, and that you know git remote -v, git fetch, and git merge from earlier chapters. Here you add a second remote to a repository and use it to stay current with a project you forked.
Why a second remote
A remote is only a name for a URL. A repository can have as many as you need. The most common reason for a second one is a fork.
When you fork a project on GitHub, you get your own copy under your account. You clone that copy, so origin points to your fork. You can push to your fork, but you can't push to the original project. When the original gains new commits, your fork does not receive them automatically. To get them, you add the original project as another remote. By convention, that remote is called upstream.
The name upstream is a convention, like origin. Git attaches no special meaning to it. Don't confuse it with an upstream tracking branch, which is the branch that git push -u records for a local branch. The two terms are unrelated.
Add the remote
Use the same command you used for origin, with a different name and the original project's URL:
git remote add upstream [email protected]:ORIGINAL-OWNER/ORIGINAL-REPO.git
git remote -vThe output lists both remotes:
origin [email protected]:YOUR-USERNAME/YOUR-REPO.git (fetch)
origin [email protected]:YOUR-USERNAME/YOUR-REPO.git (push)
upstream [email protected]:ORIGINAL-OWNER/ORIGINAL-REPO.git (fetch)
upstream [email protected]:ORIGINAL-OWNER/ORIGINAL-REPO.git (push)If you don't have write access to the original project, pushes to upstream will fail. You can block them by setting a push URL that cannot work:
git remote set-url --push upstream DISABLEDBring in upstream changes
Fetch the original project's branches. Git stores them as remote-tracking branches such as upstream/main. Nothing in your own branches changes yet.
git fetch upstream
git switch main
git merge upstream/main
git push origin mainThe merge updates your local main with the original project's commits. The push then copies them to your fork. If your local main has no commits of its own, the merge is a fast-forward and creates no merge commit.
You can use git rebase upstream/main instead of git merge. Rebase gives a linear history, but it rewrites your local commits. Avoid it on branches that others already use.
Choose where each command goes
With two remotes, a command that omits the remote uses the branch's upstream tracking setting. That setting is usually origin. Naming the remote explicitly avoids mistakes.
| Task | Command |
|---|---|
| Get new commits from the original project | `git fetch upstream` |
| Publish your work to your fork | `git push origin BRANCH` |
| See which remote a branch tracks | `git branch -vv` |
| Remove the second remote | `git remote remove upstream` |
Common problems
error: remote upstream already exists. The name is taken. Check it withgit remote -v. Then usegit remote set-url upstream NEW-URLto change it, or pick another name.fatal: 'upstream' does not appear to be a git repository. The remote has not been added in this repository, or the name is misspelled.Permission denied (publickey)on fetch. Your SSH key does not authenticate to GitHub. Runssh -T [email protected]to test it. Public repositories can also be fetched over HTTPS without credentials.
Exercise
You forked a project and cloned your fork. You ran git remote add upstream <original-url> and git fetch upstream. Which commands bring the original project's main into your local main and then update your fork on GitHub?
Show answer
git switch main, then git merge upstream/main, then git push origin main. The fetch only downloads the commits into upstream/main. The merge applies them to your local main, and the push to origin copies them to your fork.