Sync a fork with upstream

Medium risk
What do risk levels mean?
Read-only
Inspects state without changing anything.
Low risk
Reversible with a routine follow-up command.
Medium risk
Changes state; undo path documented.
Destructive
Deletes or overwrites; confirmation required.

Brings a fork's default branch up to date with the original repository by fetching upstream and fast-forwarding the local and fork branches.

gitforkupstreamsync

Parameters

Default branch to synchronize.

Commands

Confirm an upstream remote pointing at the original repository exists

git remote -v

Download the latest upstream history

git fetch upstream

Move to the branch being synchronized

git switch main

Fast-forward the local branch to the upstream tip without a merge commit

git merge --ff-only upstream/main

Update the fork on the remote

git push origin main

Verification

git log --oneline -1 upstream/main

The local branch tip matches the upstream tip shown here.

Undo

Restore the local branch to its position before the fast-forward

git reset --hard 'HEAD@{1}'

Pitfalls

  • If no upstream remote exists yet, add it first with git remote add upstream <url>.
  • The fast-forward fails when the fork branch has its own commits; rebase them onto upstream instead.

Related