How to Update or Sync a Forked Repository

When you fork a repository on platforms like GitHub, you create a personal copy of someone else's project. This is useful for making your own changes without affecting the original project. However, over time, the original repository (often called the "upstream" repository) may receive updates that your fork doesn't have. Keeping your fork in sync with the upstream repository is an important step to ensure you're working with the latest version. Here's how to do it.

Setting Up the Upstream Repository

  • Navigate to Your Cloned Repository: Open your terminal and go to your local copy of the forked repository.
  • Link to the Upstream Repository: By default, your local repository isn't connected to the original upstream repository. You need to add a new remote that points to the original repository:
git remote add upstream

Replace ORIGINAL_OWNER and ORIGINAL_REPOSITORY with the relevant information.

  • Verify the New Remote: To ensure that the upstream repository was added correctly, list all configured remotes:
git remote -v

Syncing Your Fork

  • Fetch from the Upstream Repository: First, fetch the updates from the upstream repository:
git fetch upstream
  • Switch to Your Local Main Branch: Before merging the changes, switch to your local main branch (or the branch you wish to update):
git checkout main
  • Merge Upstream Changes: Merge the changes from the upstream main branch into your local main branch:
git merge upstream/main

Regularly Syncing Your Fork

After the initial setup, syncing your main branch with the upstream repository is simple. Just run the following command whenever you need to update your fork:

git pull upstream main

Subscribe to GIT.WTF!?!

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]