Forks and multiple remotes
Contribute to a repo you can't push to: sync your fork through an upstream remote, branch from it, and set push defaults so you never push to the wrong place.
Forks and multiple remotes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A fork is your own server-side copy of someone else's repository, and you clone the fork, so `origin` is your copy and not the project.
- Add the original project as a second remote, conventionally named `upstream`, and `git fetch upstream` brings in its new commits.
- Keep your fork's `main` in sync with `git merge --ff-only upstream/main` followed by `git push origin main`, and do your work on branches.
- `git switch -c <branch> upstream/main` starts a branch from the project's latest state, and `git push -u origin <branch>` publishes it to your fork.
- `remote.pushDefault` and a per-remote `pushurl` stop you pushing to the project by accident.
When you can read a repository but not write to it
You find a bug in a project’s README. You can clone it, but you can’t push to it, because you aren’t a maintainer. Giving every stranger write access would be absurd.
The answer is a fork: a copy of the project that lives on the hosting service under your account. You can push to it freely. When your change is ready, you ask the maintainers to pull it from your copy. That request is a pull request, covered in the GitHub collaboration group.
Forking itself is a feature of the hosting service. Git only sees two ordinary repositories. That means you can rehearse the entire flow locally with two bare repositories, which is what I did, on Git 2.54. hub.git plays the project, and fork.git is a copy made with git clone --bare hub.git fork.git.
Two remotes
You clone your fork, so Git names that origin. The project needs its own name:
$ git clone fork.git contrib && cd contrib
$ git remote add upstream ../hub.git
$ git remote -v
origin .../fork.git (fetch)
origin .../fork.git (push)
upstream ../hub.git (fetch)
upstream ../hub.git (push)
By convention the original is upstream. (In this lesson “upstream” means this remote. Elsewhere, “upstream branch” means the tracking relationship from the previous lessons. Context tells them apart, but it is a famously confusing overlap.)
Staying in sync
The maintainer commits. Your fork knows nothing about it until you fetch from upstream:
$ git fetch upstream
From ../hub
* [new branch] main -> upstream/main
$ git status -sb
## main...origin/main
The status says “in sync” because it compares main to origin/main, your fork, which has not changed. The project moved, not your fork. Bring it forward, and then push to the fork:
$ git merge --ff-only upstream/main
Fast-forward
README.md | 1 +
$ git push origin main
70a4eb4..65f797e main -> main
--ff-only is the right guard. Your fork’s main should be an exact mirror of the project’s, so if it can’t fast-forward, you committed on main by mistake, and Git tells you instead of creating a merge commit. This is also why you do your work on branches.
Branch from upstream, push to your fork
$ git switch -c fix-typo upstream/main
$ git branch -vv
* fix-typo 65f797e [upstream/main] Maintainer: docs
$ git commit --allow-empty -m "Fix typo"
$ git push -u origin fix-typo
* [new branch] fix-typo -> fix-typo
branch 'fix-typo' set up to track 'origin/fix-typo'.
Look at the first branch -vv. Switching from a remote-tracking branch made upstream/main this branch’s upstream, so a bare git push would aim at the project. The explicit git push -u origin fix-typo fixed that: the final upstream is origin/fix-typo, your fork.
Guard rails
Two settings stop you pushing to the wrong remote.
$ git config remote.pushDefault origin
$ git remote set-url --push upstream DISABLED
$ git remote -v
origin .../fork.git (fetch)
origin .../fork.git (push)
upstream ../hub.git (fetch)
upstream DISABLED (push)
$ git push upstream fix-typo
fatal: 'DISABLED' does not appear to be a git repository
remote.pushDefault makes origin the destination for pushes. The pushurl on upstream replaces only the push address with a nonsense value, so fetching still works and pushing fails loudly. Even if you could push to the project, doing it directly is usually not what its process wants.
Other multi-remote commands you will use:
git fetch --allfetches every remote.git remote rename upstream hubrenames one. The tracking branches follow.git remote remove <name>forgets one.
Multiple remotes aren’t only for forks. A repository can push to a staging server and a hosting service, or mirror to a backup.
What the video covers
CodeLucky’s “How to Sync Your Fork with an Upstream Repository in Git (2026)” is a short video, about three minutes, from around 2025. From its title it covers adding an upstream remote and syncing a fork, the core of this lesson. At three minutes it can’t cover branching from upstream/main or the push guard rails, which come from my own runs. I judge English from the title, description and channel only, and I could not read a transcript. Faraday Academy’s longer but older (about 2020) video is linked as an alternative.
Where this fits
That closes the remotes group. Next you start rewriting your own history, beginning with the most flexible tool for it: interactive rebase.
Check yourself
In a fork workflow, what are `origin` and `upstream`?
`origin` is your fork, which you can push to. `upstream` is the original project, which you usually can only fetch from.
How do you update your fork's `main` with the project's newest commits?
Fetch `upstream`, run `git merge --ff-only upstream/main` on your `main`, then `git push origin main`.
Why start a feature branch from `upstream/main` instead of your fork's `main`?
It begins from the project's real latest state, so your pull request is based on current code and doesn't conflict with changes you hadn't fetched.
What does `git fetch --all` do?
It fetches from every configured remote in turn, updating all the remote-tracking branches.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.