Sub topic 12
Keep your fork in sync with upstream
This chapter assumes you have a fork cloned locally with origin and upstream remotes configured, and that you know git fetch, git merge, and git switch. Your fork does not update itself. While you work, the original project keeps moving, and your fork's main falls behind.
Why your fork drifts
A fork is a copy made at one moment. When maintainers merge other people's pull requests into the original repository, those commits exist only in upstream. Your origin (the fork on GitHub) and your local clone know nothing about them until you pull them in.
Stale forks cause two problems. New branches start from old code, and your pull request may conflict with newer changes.
The sync steps
Run these from inside your clone:
git fetch upstream
git switch main
git merge --ff-only upstream/main
git push origin mainEach command does one job:
git fetch upstreamdownloads new commits and updates theupstream/mainreference. It does not change your files or branches.git switch mainmoves you to your localmain.git merge --ff-only upstream/mainmovesmainforward to match upstream. A fast-forward only moves the branch pointer, so no new merge commit appears.git push origin maincopies the updatedmainto your fork on GitHub.
Replace main with the project's default branch name if it differs, such as master or develop.
If the fast-forward fails
The error usually reads fatal: Not possible to fast-forward, aborting. It means your main has diverged, typically because you committed directly on main.
Compare the two branches first:
git log --oneline upstream/main..mainThis lists commits that exist on your main but not upstream. If they were made by mistake, move them to a new branch before resetting:
git branch rescue-work
git reset --hard upstream/mainThe rescue-work branch keeps your commits. The reset --hard then discards uncommitted changes and points main at upstream, so check git status first. To avoid this situation, do your work on feature branches and leave main untouched.
Syncing from the website
GitHub's fork page has a Sync fork button that updates your fork's main on GitHub. This is quick, but your local clone is still behind. Run git fetch origin and git merge --ff-only origin/main on main afterwards.
Updating a branch you are working on
Syncing main does not change your feature branches. To build on the latest code, rebase or merge the updated main into your branch. The next chapter covers rebasing and conflict resolution.
Common mistakes
| Mistake | Result | Fix |
|---|---|---|
| Skipping `git fetch upstream` | `upstream/main` is outdated, so nothing new is merged | Fetch first, every time |
| Committing on `main` | Fast-forward fails | Move commits to a branch, then reset `main` |
| Forgetting `git push origin main` | Your fork on GitHub stays stale | Push after merging |
| Using the wrong branch name | `upstream/main` not found | Check with `git branch -r` |
Exercise
You run git merge --ff-only upstream/main and Git reports that it cannot fast-forward. What does this tell you, and what command would you run to see the cause?
Show answer
Your local main has commits that upstream does not have, so the branches have diverged. Run git log --oneline upstream/main..main to list those commits.
Exercise
After you click Sync fork on GitHub, why does your local main still lack the new commits, and how do you fix it?
Show answer
The button only updates the fork on GitHub (origin). Your local clone must fetch and merge: run git fetch origin, then git switch main and git merge --ff-only origin/main.