Fork something? Update your fork with changes from upstream
I fork a LOT of things I see on GitHub. It’s habit, most I never touch, but others I do keep up on.
It’s nice to be able to merge changes that the original authors made, back into my fork. Especially if a major feature was added in after I made my fork. Keeping your forked GitHub repository in sync with that of the original repository requires you to add a new git remote, merge the upstream code with your fork, and then push master back to your fork on GitHub.
Here’s how it’s all done
First, you’ll need to add an
git remote add upstream https://github.com/user/repo.git
There’s really only two steps involved. Fetching updates from the newly created
upstream remote and then merging the changes into your code.
To fetch updates from the
upstream remote, do this:
git fetch upstream
Now we’ve got all the changes made to the original “forked” repository. All the changes are stored in a local branch,
Now, make sure you’re on the branch you’re supposed to be, if you don’t know, you want
master. To ensure you’re on the local master branch, do this:
git checkout master
Now that we’re on our local master branch, we can merge the changes from the original repository. To merge from the
upstream/master branch to your local
master branch, do this:
git merge upstream/master
That’s it! You can now update your fork on GitHub with updates from the repository you originally forked.
git push origin master
Now, at this point, your fork on GitHub should contain changes from the “original” repository that you forked, as well as any changes that you made to your own fork. Keep an eye on the “original” repository and perform these steps again, except for adding the
upstream remote, you’ll only need to do that once.
For more information, checkout this piece from GitHub Help.