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
- Add upstream remote:
git remote add upstream https://github.com/original/repo.git
- Fetch upstream changes:
git fetch upstream
- Merge into your main:
git checkout main
git merge upstream/main
- 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.