How to update a forked Github repository

It is not unusual for a number of forks to accumulate in your Github account. If you spend some time working on a fork, you will first want to update it, as the author of the original may have made changes in the meantime. This blog post describes how to do this in the shell.

  1. Open Terminal (or iTerm, for example) and use cd to change to the local repository.
  2. Make sure the correct branch is activated. Typically this will be master — or main for newer repositories.
$ git checkout main
  1. Before the update can be performed, it is necessary to check whether the original repository (the original upstream) is known. This can be checked with the following command:
$ git remote -v
origin    https://github.com/xern/imessage_reader.git (fetch)
origin    https://github.com/xern/imessage_reader.git (push)
  1. In this case, only the forked repository is displayed. Therefore, it is necessary to add the original repository, which is achieved with the instruction shown below, adjusting the original address within <> accordingly.
$ git remote add upstream <https://github.com/original_owner/original_repository.git>
  1. Now, if you run git remote -v, you will get the following output:
origin    https://github.com/xern/imessage_reader.git (fetch)
origin    https://github.com/xern/imessage_reader.git (push)
upstream    https://github.com/niftycode/imessage_reader.git (fetch)
upstream    https://github.com/niftycode/imessage_reader.git (push)
  1. Now it’s time to fetch the changes:
$ git fetch upstream
  1. The changes can then be applied to the forked repository:
$ git merge upstream/main
  1. Finally, the changes are uploaded to your own remote fork with push:
$ git push

After these steps are completed, the original repository and the fork are back in sync.