How to Keep Your GitHub Fork in Sync

How to Keep Your GitHub Fork in Sync

When you fork a project on GitHub, changes to the original repository do not automatically sync with yours. This can lead to merge conflicts, or when you create a new branch for a PR not having the latest version. Updating your fork can be done with just a few commands.

Initial Setup

If you haven't already done so you will need to set the upstream url for your local environment.

Note: replace original-author with the username that you forked from, and repo-name with the repository name.

git remote add upstream https://github.com/original-author/repo-name

Updating Your Fork

Whenever you wish to update your fork, follow the steps below.

Note: replace master with the default branch name if required.

  1. Switch to the main branch

    git checkout master
    
  2. Fetch changes from the original repo.

    git fetch upstream
    
  3. Merge changes into local environment.

    git merge --ff-only upstream/master
    
  4. Push changes into fork.

    git push origin master
    

Updating Your Branch

If you started working in a new branch and then changes have come in that you would like to merge, follow the steps below.

Note: replace my-feature with your branch name, and master with the default branch name if required.

  1. Switch to your branch.

    git checkout my-feature
    
  2. Rebase on to the main branch.

    git rebase master
    
  3. Resolve any conflicts if needed.

  4. Push your updated branch.

    git push --force origin my-feature