De replendo Gitis exemplaris mutationibus originis

How to merge upstream changes into a local Git repository.

This post is a short reminder1 about how to merge a repository’s upstream changes into a local branch.

Assume we cloned a Git repository named res, created our own development branch localdev, made some changes, and commited them to localdev. At the same time, the (upstream) developers of res modified their version and commited the changes to the branch master. We want our modified version of res to reflect the changes made upstream, hence we need to merge the latest changes of master into localdev.

Therefore, we need to ascertain that our clone tracks res’ remote repository.2 If it does not, we assign our local copy of res a remote tracking branch pointing to res’ remote repository:

git remote add upstream https://link/to/res.git

This being done, we can proceed with the update of our local Git repository.

First, fetch all changes from the upstream remote tracking branch of res:

git fetch upstream

Second, navigate to the local branch into which upstream changes are to be incorporated. As we want to merge the changes into localdev, our target branch is localdev:

git checkout localdev

Third, merge the changes of the remote tracking branch upstream/master into localdev:3

git merge upstream/master

Fourth, if necessary, resolve any merge conflicts, and create a new commit containing the current state of the repository.

For more information about Git, see:

  1. Hence the latin title, which may or may not have taken more time to decide upon than to write the rest of this post. [return]
  2. We can check this by calling git remote -v. If the returned list is empty, no remote has been added until now. [return]
  3. For a helpful overview of how to work with remote branches, see e.g. chapter 3.5 of Chacon, Scott, and Ben Straub. 2019. Pro Git. 2nd Edition. Apress: New York. [return]