Configuring a remote for a fork
Git is designed to give each developer an entirely isolated development environment. This means that information is not automatically passed back and forth between repositories. Instead, developers need to manually pull upstream commits into their local repository or manually push their local commits back up to the central repository. We must configure a remote that points to the upstream repository in Git to sync changes we make in a fork with the original repository. This also allows us to sync changes made in the original repository with the fork. This is something I often do but rarely remember the steps for. This post is intended to serve as a reminder for me and anyone else having the same question; how to configure a remote for a fork or how to add an upstream remote git repository?
You can start by forking the repository you are contributing to and cloning that repository to your local file system. For this demonstration, let’s use the Apache Flink repository and assume you have cloned it locally.
git clone https://github.com/sarathkumarsivan/flink.git
If you would like to list the current configured remote repository for your fork before configuring the upstream, issue the below command:
git remote -vorigin https://github.com/sarathkumarsivan/flink.git (fetch)
origin https://github.com/sarathkumarsivan/flink.git (push)
Now, we can add the upstream remote using the following command:
git remote add upstream https://github.com/apache/flink.git
Now we’ve a remote repository configured. Let’s verify the new upstream repository we’ve specified for our fork.
git remote -vorigin https://github.com/sarathkumarsivan/flink.git (fetch)
origin https://github.com/sarathkumarsivan/flink.git (push)
upstream https://github.com/apache/flink.git (fetch)
upstream https://github.com/apache/flink.git (push)
You can also fetch all branches since the remote repository is configured:
git fetch upstream
From here, you can checkout a branch to base your work off of. In many cases, this means rebasing your changes onto the upstream branch:
git rebase upstream/master
When trying to sync a fork that does not have an upstream set you may get output that looks like the following:
$ git fetch upstream
fatal: 'upstream' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
The way to overcome this issue would be to check that you have an upstream and sync it as show above. This is basic git stuff, but since I always end up searching for it, I thought it would be useful to write it down for future reference.