Sub topic 15
Cloning a Fork and Adding an Upstream Remote
This chapter assumes you can run git clone, know what a remote is, and understand branches and commits. It covers how to work with a fork, which is your own GitHub copy of someone else's repository.
Why a fork needs two remotes
A fork lives under your account, for example YOUR-USERNAME/REPO. You can push to it. You usually cannot push to the original repository, which belongs to OWNER.
When you clone your fork, Git creates one remote named origin that points to your fork. The original repository is not known to your clone. Over time the original gains new commits that your fork does not have. To receive them, you add a second remote. By convention it is named upstream.
| Remote | Points to | You can push? | Purpose |
|---|---|---|---|
| `origin` | Your fork | Yes | Publish your branches, open pull requests |
| `upstream` | The original repository | Usually no | Fetch the latest changes |
Clone your fork and add upstream
First create the fork on GitHub with the Fork button on the original repository's page. Then clone your fork, not the original:
git clone https://github.com/YOUR-USERNAME/REPO.git
cd REPO
git remote add upstream https://github.com/OWNER/REPO.git
git remote -vReplace YOUR-USERNAME, OWNER, and REPO with real names. The last command lists every remote. You should see four lines, a fetch and a push entry for each of origin and upstream:
origin https://github.com/YOUR-USERNAME/REPO.git (fetch)
origin https://github.com/YOUR-USERNAME/REPO.git (push)
upstream https://github.com/OWNER/REPO.git (fetch)
upstream https://github.com/OWNER/REPO.git (push)If you cloned over SSH, use the SSH form of both URLs so that one authentication method covers both remotes.
Pulling in the original's changes
Once upstream exists, fetch from it. The fetch downloads commits and updates remote-tracking branches such as upstream/main. It does not change your working files.
git fetch upstream
git switch main
git merge upstream/main
git push origin mainThe merge brings your local main up to date with the original. The push then copies that update to your fork on GitHub. If the default branch is not called main, use its real name. Check with git branch -a.
The GitHub CLI can set up the same layout in one step. Running gh repo fork --clone forks the repository, clones your fork, and adds the original as upstream. This depends on your gh version, so confirm the result with git remote -v.
Common mistakes
- Cloning the original instead of your fork.
originthen points to a repository you cannot push to, andgit pushfails with a permission error. Fix it by forking first, then changing the remote:git remote set-url origin https://github.com/YOUR-USERNAME/REPO.git. - Skipping the fetch.
upstream/mainis only a local record of the last fetch. It is stale until you rungit fetch upstream. - Committing directly on
main. Your localmainthen diverges fromupstream/main, and merges get messy. Do your work on a feature branch and keepmainas a clean mirror of the original. - Mistyping the remote URL.
git remote adddoes not check that the URL exists. The error appears at the first fetch. Correct it withgit remote set-url upstream NEW-URL.
Exercise
You cloned your fork and ran git remote add upstream https://github.com/OWNER/REPO.git. A maintainer merged new commits into the original repository this morning. Which commands bring those commits into your local main and then into your fork on GitHub?
Show answer
Run git fetch upstream, then git switch main, then git merge upstream/main, then git push origin main. The fetch downloads the new commits, the merge updates local main, and the push publishes the result to your fork (origin).
Exercise
After cloning, git push fails with a permission denied error, and git remote -v shows origin pointing to OWNER/REPO. What went wrong and how do you fix it?
Show answer
You cloned the original repository instead of your fork. Fork it on GitHub, then run git remote set-url origin https://github.com/YOUR-USERNAME/REPO.git. Add the original back as upstream with git remote add upstream https://github.com/OWNER/REPO.git.