Before you can sync, you need to add a remote that points to the upstream repository. You may have done this when you originally forked.
$ git remote -v
# List the current remotes
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
$ git remote add upstream https://github.com/otheruser/repo.git
# Set a new remote
$ git remote -v
# Verify new remote
origin https://github.com/user/repo.git (fetch)
origin https://github.com/user/repo.git (push)
upstream https://github.com/otheruser/repo.git (fetch)
upstream https://github.com/otheruser/repo.git (push)
There are two steps required to sync your repository with the upstream: first you must fetch from the remote, then you must merge the desired branch into your local branch.
Fetching from the remote repository will bring in its branches and their respective commits. These are stored in your local repository under special branches.
$ git fetch upstream
# Grab the upstream remote's branches
We now have the upstream's main
branch stored in a local branch, upstream/main
.
$ git branch -va
# List all local and remote-tracking branches
Now that we have fetched the upstream repository, we want to merge/rebase its changes into our local branch. This will bring that branch into sync with the upstream, without losing our local changes.
$ git checkout main
# Check out our local main branch
Do git rebase
or merge
.
$ git rebase upstream/master
# or
$ git merge upstream/master
If your local branch didn't have any unique commits, git will instead perform a "fast-forward".
Source: https://stackoverflow.com/questions/7244321/how-do-i-update-or-sync-a-forked-repository-on-github