Merge upstream changes with Git

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:

  1. 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.
  2. Next browse to the root folder of your local repository in your shell of preference.
  3. Execute the command:

    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:

  1. First you need to fetch all of the latest changes in the upstream repository be executing:
  2. Make sure your repository is on the correct branch by executing:
  3. Now simply merge the changes by executing:

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.

Leave a Reply

Your email address will not be published. Required fields are marked *