Skip to content

Instantly share code, notes, and snippets.

@eXsoR65
Forked from YakDriver/sync.md
Created May 6, 2022 17:56
Show Gist options
  • Save eXsoR65/0284bac053a6b9defd6f098e0a89edf8 to your computer and use it in GitHub Desktop.
Save eXsoR65/0284bac053a6b9defd6f098e0a89edf8 to your computer and use it in GitHub Desktop.
Re-sync a fork with the upstream repo

First, you want to make sure that your upstream is setup. Here origin is your fork and upstream is the forked repo.

$ git remote -v
origin	https://github.com/YakDriver/watchmaker.git (fetch)
origin	https://github.com/YakDriver/watchmaker.git (push)
upstream	https://github.com/plus3it/watchmaker.git (fetch)
upstream	https://github.com/plus3it/watchmaker.git (push)

If you don't have the main repo, then add the remote (i.e., upstream) repo. You can call it anything but convention says that the repo you own (i.e., your fork) is the origin while the main repo (i.e., the one you want to submit a PR to) is upstream.

$ git remote add upstream https://github.com/user/repo.git

Now with upstream setup, you can sync the two when changes happen on the upstream repo that you want to bring back to your repo.

NOTE: There are two ways to do this. One is a hard reset. You will lose all changes to origin. This might be good if you messed up your fork and want to have a reset. I've also found this handy when my origin gets hosed up.

The HARD Reset

develop here is the name of the branch you want to reset. The branch must exist in both origin and upstream.

$ git fetch --all
$ git checkout develop
$ git reset --hard upstream/develop
$ git push -f

The SOFT Reset

This will apply any commits that have been applied to upstream and apply them to your repo.

develop is the name of the branch you want to reset. The branch must exist in both origin and upstream.

$ git checkout develop
$ git pull upstream develop
$ git push
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment