Code Reference

Sync Fork Branch

Git · Example / how-to

Sync Fork Branch

Git · Example / how-to


📋 Overview

Update your fork’s branch from the upstream repository using remotes, fetch, and rebase or merge.

🔧 Core concepts

PieceRole
upstream remoteOriginal repo
origin remoteYour fork
fetchDownload without merge
rebase / mergeIntegrate upstream

💡 Examples

One-time remote setup:

git remote add upstream https://github.com/ORG/REPO.git
git remote -v

Sync main, then update feature branch:

git fetch upstream
git checkout main
git merge --ff-only upstream/main
git push origin main

git checkout feature/my-work
git rebase main
# or: git merge main
git push origin feature/my-work

If rebase rewrote commits already on the fork:

git push --force-with-lease origin feature/my-work

⚠️ Pitfalls

  • Prefer --force-with-lease over --force to avoid clobbering others’ pushes.
  • Confusing origin and upstream pushes updates to the wrong place.
  • Long-lived forks: sync often to reduce conflict size.

On this page