Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Keeping a fork up to date

1. Clone your fork:

git clone git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git

By default, the remote origin would be set to git@github.com:YOUR-USERNAME/YOUR-FORKED-REPO.git Now add a new remote to point to the original repository from which you forked.

2. Add remote from original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream

3. Updating your fork from original repo to keep up with their changes:

git merge upstream/master

or

git pull upstream master

(git pull is actually git fetch + git merge) If the origin and upstream have a linear path, both the above commands will do a fast-forward merge (no merge commit). If the path is not linear, i.e. if the origin has some commits that are not in upstream, fast-forwarding cannot be done.

4. Merge commits

If accidentally because of some change in your origin, when you pull upstream, a merge commit might have been created. Once you create the first merge commit, all future sync merges will be prevented from fast-forwarding because you have a commit that is unique to your fork that does not exist on the upstream repository.

At that point, you might see the status of your branch as X commit ahead of upstream To get your fork back again in sync with upstream,

git fetch upstream
git reset --hard upstream/master
git push --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment