How to sync a fork with upstream in Git

· Category: Git

Short answer

Fetch from the upstream remote and merge or rebase its changes into your local branch.

Steps

  1. Add upstream remote:
git remote add upstream https://github.com/original/repo.git
  1. Fetch upstream changes:
git fetch upstream
  1. Merge into your main:
git checkout main
git merge upstream/main
  1. Push to your fork:
git push origin main

Tips

  • Use rebase instead of merge for a cleaner history.
  • Do this regularly to avoid large merge conflicts later.
  • GitHub has a "Sync fork" button that automates this.

Common issues

  • Conflicts during sync: resolve them as you would with any merge.
  • Divergent history on your fork: consider resetting or cherry-picking.