How Do I Update or Sync a Forked Repository on Github?

Better Stack Team
Updated on June 24, 2024

To update or sync a forked repository on GitHub with its original upstream repository (the one you forked from), you need to perform a few steps. Here's how you can do it:

Step 1: Add the Upstream Remote

First, you need to add the original upstream repository as a remote to your forked repository. You typically only need to do this once:

 
git remote add upstream <upstream-repository-url>

Replace <upstream-repository-url> with the URL of the original upstream repository. You can usually find this URL on the original repository's GitHub page.

Step 2: Fetch Changes from Upstream

Fetch the changes from the upstream repository:

 
git fetch upstream

This command will fetch all branches and commits from the upstream repository.

Step 3: Merge or Rebase Changes

Now, you can merge or rebase the changes from the upstream repository into your local branch. You have two options:

Option 1: Merge Changes

 
git checkout master  # or the branch you want to update
git merge upstream/master

This command will merge the changes from the upstream repository's master branch into your local branch.

 
git checkout master  # or the branch you want to update
git rebase upstream/master

This command will rebase your local changes on top of the changes from the upstream repository's master branch, resulting in a cleaner commit history.

Step 4: Push Changes to Your Fork

Finally, push the merged or rebased changes to your forked repository on GitHub:

 
git push origin master  # or the branch you updated

This command will push the changes to your forked repository on GitHub, updating it with the changes from the upstream repository.

Note:

  • You may need to resolve any merge conflicts that occur during the merge or rebase process.
  • Make sure you have permission to push changes to your forked repository on GitHub.
  • It's a good practice to keep your forked repository updated with the changes from the upstream repository to stay in sync with the latest developments.
Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

community@betterstack.com

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github