More Git answers

by Danny
Published 2 min read

Add the original repository as an upstream remote, fetch it, then rebase or merge its main branch into yours and push. Here is how, plus the one-click options on GitHub.

The short answer

#

Your fork does not update itself. Add the original project as a second remote, usually called upstream, fetch from it, bring its changes into your branch, and push to your fork.


_10
git remote add upstream https://github.com/original-owner/project.git
_10
git fetch upstream
_10
git switch main
_10
git merge --ff-only upstream/main
_10
git push origin main

Why it happens

#

A fork is a full copy of the repository taken at one moment. After that, it is a separate repository. New commits in the original project do not flow into your fork, and your local clone only knows about the remote you cloned from, which is your fork (origin).

To get the new commits, your clone needs to know where the original lives. That is all the upstream remote is: a second named URL. Once it is fetched, upstream/main is a normal remote branch you can merge or rebase onto, just like origin/main.

How to fix it

#

Set up the upstream remote once

Check your remotes first. You will normally see only origin.


_10
git remote -v
_10
git remote add upstream https://github.com/original-owner/project.git
_10
git remote -v

If the original project's default branch is not main, use its name in the commands below.

Keep main as a clean copy of upstream

The tidiest workflow is to never commit directly to main in your fork. Then updating it is always a fast-forward, with no merge commits or conflicts.


_10
git fetch upstream
_10
git switch main
_10
git merge --ff-only upstream/main
_10
git push origin main

--ff-only refuses to create a merge commit. If it fails, you have commits on main that upstream does not, which is a sign they belong on a feature branch.

Update a feature branch with rebase

For a branch you are preparing as a pull request, rebasing onto the latest upstream keeps your commits on top and the history linear.


_10
git fetch upstream
_10
git switch my-feature
_10
git rebase upstream/main
_10
# fix any conflicts, then: git add <file> && git rebase --continue
_10
git push --force-with-lease origin my-feature

Rebasing rewrites your branch's commits, so the push needs --force-with-lease. It refuses to overwrite the remote if someone else pushed to it since you last fetched, which plain --force does not check.

Update a feature branch with merge

If other people share the branch, or the project prefers merges, merge instead. Nothing is rewritten, so a normal push works.


_10
git fetch upstream
_10
git switch my-feature
_10
git merge upstream/main
_10
git push origin my-feature

Use GitHub's sync button or the gh CLI

On GitHub, the fork's page has a Sync fork button that updates the fork's branch from the original on the server. You still need to pull afterwards to update your local clone.

With the GitHub CLI, you can do the same from the terminal.


_10
gh repo sync your-name/project
_10
git pull

These options work best when your branch has no commits of its own. If it has diverged, use the rebase or merge approach above so you can resolve conflicts locally.

Common pitfalls

#
  • Pushing to upstream by accident. You usually cannot, but if you have write access, double-check the remote name before pushing. Some people disable it with git remote set-url --push upstream no-push.
  • Committing on main. It makes every sync a merge. Keep your work on branches.
  • Using --force instead of --force-with-lease****. Plain force can silently delete commits pushed by someone else.
  • Forgetting to fetch. upstream/main only updates when you run git fetch upstream.
  • Renamed default branch. If upstream moved from master to main, update your local branch and its tracking to match.
#

Was this resource helpful?