Tag Archives: Forking

How to catch up my git fork to master


git fetch upstream
git checkout master
git merge upstream/master

git_logoHere I am in one of my forked git repositories on GitHub. I have a patch to contribute, or a bug to fix but I want to make sure that my local copy of the repository is not stale. If I do not, the project I want to contribute to might not be able to apply my patch or merge my pull request cleanly. The first thing I need to do is to catch up my git repository to whatever the current code is in the master branch of the original repository. Some development workflows will use a different branch than master for day-to-day development but the same steps apply using whatever that branch name is. In this post, I’ll assume you are using master. You’ll also need a git command line tool.

There are two steps required. First, you must configure a git remote for a fork. Then you can catch up that fork to the current master.

Open a command line prompt and change the current directory to your project’s directory.

Configuring a git remote for a fork

You only need to do this once: Add a new remote upstream repository to sync with the fork where ORIGINAL_OWNER is the original GitHub account and ORIGINAL_REPOSITORY is the original repository name.

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

For example:

git remote add upstream https://github.com/apache/thrift.git

You can verify that all went well:

git remote -v
origin https://github.com/garydgregory/thrift.git (fetch)
origin https://github.com/garydgregory/thrift.git (push)
upstream https://github.com/apache/thrift.git (fetch)
upstream https://github.com/apache/thrift.git (push)

Catching up a git fork to master

Fetch project branches from the upstream repository to get all the commits. Your commits to master will be stored in the local branch upstream/master.

git fetch upstream

Check out the master branch from your local fork.

git checkout master

Now merge the changes from upstream/master into your local master branch. Your fork’s master branch will be in sync with the upstream repository. You will not lose your local changes.

git merge upstream/master

All done!

You can cut and paste this into a script:

git fetch upstream
git checkout master
git merge upstream/master

Happy Forking,