Sub topic 5
Set up the origin and upstream remotes
This chapter assumes you have a GitHub account, Git installed with your name and email configured, and basic comfort with add, commit, status, and log in a terminal. You should also have forked a repository and cloned your fork. The previous chapters covered both steps.
What a remote is
A remote is a named bookmark for another copy of your repository, stored as a URL. Git uses these names so you do not have to type a full URL for every fetch or push.
A fork workflow needs two remotes:
| Remote | Points to | You can push? | Typical use |
|---|---|---|---|
| `origin` | Your fork on GitHub | Yes | Push your feature branches |
| `upstream` | The original project | Usually no | Fetch the latest changes |
When you run git clone on your fork, Git creates origin automatically. It does not create upstream. You add that one yourself.
Add the upstream remote
Run these commands from inside your clone. Replace the placeholders with real names.
git clone https://github.com/YOUR-USERNAME/REPO.git
cd REPO
git remote add upstream https://github.com/ORIGINAL-OWNER/REPO.git
git remote -vgit remote add upstream <url> registers the name upstream. The name is a convention, not a keyword. Git would accept any name, but most projects and guides use upstream, so following it makes your setup easy for others to read.
Verify the result
git remote -v lists each remote with its URL, once for fetching and once for pushing. The output looks like this:
origin https://github.com/YOUR-USERNAME/REPO.git (fetch)
origin https://github.com/YOUR-USERNAME/REPO.git (push)
upstream https://github.com/ORIGINAL-OWNER/REPO.git (fetch)
upstream https://github.com/ORIGINAL-OWNER/REPO.git (push)Check two things. The origin URL must contain your username, and the upstream URL must contain the original owner's name. If they are swapped, you cloned the wrong repository, and your pushes will fail or go to the wrong place.
Common errors
error: remote upstream already exists. The remote is already configured. Rungit remote -vto check its URL. If the URL is wrong, fix it withgit remote set-url upstream <correct-url>.fatal: not a git repository. You ran the command outside the clone. Usecd REPOfirst.- Typo in the URL. Nothing fails when you add the remote. The error appears later, at your first
git fetch upstream. Runninggit remote -vright away catches typos early. - Swapped remotes. If
originpoints to the original project, remove the remote withgit remote remove <name>and add it again with the correct URL.
Why this setup matters
You will push your work to origin, because that is the only place you can write. You will pull other people's work from upstream, because that is where the project moves forward. Later chapters use both remotes to keep your fork current and to rebase an open pull request.
Exercise
After cloning your fork, you run git remote -v and see only origin. Which command adds the original project as a remote named upstream, and which command confirms that the URL is correct without changing anything?
Show answer
Run git remote add upstream https://github.com/ORIGINAL-OWNER/REPO.git to add it. Then run git remote -v to list the URLs, or git fetch upstream to confirm that Git can reach the repository. Neither command changes your branches.
Exercise
Why can you run git push upstream main without a Git error on your side, yet still have it fail?
Show answer
Git only stores the URL and does not know your permissions. GitHub checks them when the push arrives, and it rejects the push because you do not have write access to the original repository.