Syncing Upstream Repositories and Keeping Forks Updated
Syncing Upstream Repositories and Keeping Forks Updated
When contributing to open-source software, hundreds of developers may merge code into the original project every week. If your personal fork falls behind, you will encounter merge conflicts and outdated code. Keeping your fork in sync with the original repository (called upstream) is a vital skill.
1. The Multi-Remote Topology: origin vs upstream
When you work on a fork, your local machine communicates with two different remote repositories:
origin: Points to your personal fork on GitHub. You have write/push access here.upstream: Points to the original canonical repository. You read/fetch updates from here.
2. Setting Up the upstream Remote
When you first clone your personal fork, Git only knows about origin:
Adding the Original Project as upstream:
Verify Both Remotes:
3. The 4-Step Terminal Sync Workflow
Whenever you sit down to write new code or before opening a PR, sync your local main branch with upstream:
Your local repository AND your personal fork on GitHub are now 100% up-to-date with the original open-source project!
4. Alternative: GitHub Web UI "Sync Fork" Button
GitHub also provides a convenient web button on your fork's homepage:
- 1Navigate to your fork repository on GitHub (
github.com/your-username/repo-name). - 2Under the repository header, look for:
"This branch is 12 commits behind upstream:main".
- 1Click the Sync fork button -> Update branch.
- 2Then, pull down the updates to your laptop:
Practice Quiz
upstream conventionally refer to?upstream remote?main branch?upstream/main into your local main, why should you run git push origin main?Shelving Changes Temporarily with git stash (save, pop, apply, drop)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.