Let’s say you want to work on some code in a git repository. To do this, generally you will “fork” this repository, by cloning it into your own repository. On GitHub there’s a simple “fork” feature that takes care of this. After a while, you might have made some changes, but the original repository that was forked might also have new commits pushed to it. A question many new git users then face is: “How do I pull the upstream changes from the original repository into my own?”
The fact is that git makes it really easy for you to merge upstream changes into your own cloned repository. Before you can do this, you will need to add a remote branch.
Remote branches are branches of code in outside repositories. If you want to pull changes from the original repository, you need to add it to your local repository. This is a process you only need to go through once after you clone your local repository though. Here’s a few simple steps:
- Get the link to the original git repository. This can be an SSH link as well as an HTTPS link. If your git environment is set up properly, using either one shouldn’t be an issue.
- Next browse to the root folder of your local repository in your shell of preference.
- Execute the command:
1git remote add upstream LINKHERE
Replace “LINKHERE” with the link of the original repository.
Now that you’ve added the remote branch named upstream
, you can simply merge any changes in the original repository into your own by following these steps:
- First you need to fetch all of the latest changes in the upstream repository be executing:
1git fetch upstream - Make sure your repository is on the correct branch by executing:
1git checkout master - Now simply merge the changes by executing:
1git merge upstream/master
Keep in mind, the example above assumes that you want to merge changes from the master
branch of the original repository into the master
branch of your local repository. You can of course use different branches as well.