Skip to content

Instantly share code, notes, and snippets.

@ramesaliyev
Last active June 15, 2024 12:07
Show Gist options
  • Save ramesaliyev/1a6bbd47abd9dba55dcd4d8f917b83f7 to your computer and use it in GitHub Desktop.
Save ramesaliyev/1a6bbd47abd9dba55dcd4d8f917b83f7 to your computer and use it in GitHub Desktop.
Syncing a Forked Repo

Syncing a Forked Repo

The Setup

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)

Syncing

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

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

Merging or Rebasing

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment