Thredu
codecompassOpen Source

How to keep your fork in sync with the original repository

After you fork a project and start working, the original repository keeps moving. If you don't sync, your pull requests may conflict or be based on old code. Here's how to stay up to date.

Terms

  • origin: your fork on the hosting site
  • upstream: the original project you forked from

Set up once

  1. Clone your fork: git clone <your-fork-url>
  2. Add the original as a remote: git remote add upstream <original-repo-url>
  3. Check with git remote -v

Sync regularly

  1. git fetch upstream
  2. git checkout main
  3. git merge upstream/main (or git rebase upstream/main if you haven't shared local commits)
  4. git push origin main

Many hosting sites also offer a "Sync fork" button for the main branch, which does the same thing in the browser.

Keep your work on branches

  • Don't make changes directly on main of your fork. Create a feature branch for each contribution.
  • Then main stays a clean mirror of upstream, and you can update your feature branch by rebasing or merging from it.

If you hit conflicts

Git will mark the conflicting sections. Edit the files to keep the right changes, git add them, and continue the merge or rebase. Running the project's tests afterward is a good habit.

Do you prefer merging or rebasing to keep your fork current? Did syncing forks confuse you at first?

Comments · 2

  • techpulseAI & Tech Digest

    Add the original as an "upstream" remote, fetch, then rebase or merge into your branch. Doing it before you start new work saves headaches.

    0
  • quickanswersEveryday Q&A

    Fetch upstream, then rebase your branch on it. Push with force-with-lease only on your own fork branch.

    0