By Kimserey Lam with

# Keep Github Fork Up To Date

Jul 12th, 2019 - written by Kimserey with .

When working on open-sourced projects, it is common behavior to fork a repository. The fork repository is a replica of the main repository with the only difference being that it is under our own ownership and is separated from the original repository. That allows us to make changes to the project without impacting the main repository. This scenario is very common for contributions where we make commits on our fork repository and later on submit a pullrequest against the original repository. But after having forked the repository, our own copy no longer gets new updates from the original repository. Today we will see how we can setup a forked repository to continue getting the latest commits from the original repository.

## Upstream Remote

Once we fork the repository, our remote origin will now be the fork repository. Therefore any default fetch and pull will be on that repository. To be able to fetch the main repository, we can setup an upstream remote.

1
git remote add upstream [main repository git]


For example here I have forked primeng repository:

1
2
3
$git remote -v origin https://github.com/Kimserey/primeng.git (fetch) origin https://github.com/Kimserey/primeng.git (push)  After adding the remote upstream. 1$ git remote add upstream https://github.com/primefaces/primeng.git


We can now see it on the remote list:

1
2
3
4
5
$git remote -v origin https://github.com/Kimserey/primeng.git (fetch) origin https://github.com/Kimserey/primeng.git (push) upstream https://github.com/primefaces/primeng.git (fetch) upstream https://github.com/primefaces/primeng.git (push)  Now we will be able to fetch the upstream remote which is the main repository. ## Prevent Upstream Write In order to prevent mistakes by write directly into the upstream, we can remove the push url. This will then make any write fail and prevent any mistakes. 1 git remote set-url --push upstream disabled  This command will replace the git url for push by disabled. 1 2 3 4 5$ git remote -v
origin  https://github.com/Kimserey/primeng.git (fetch)
origin  https://github.com/Kimserey/primeng.git (push)
upstream        https://github.com/primefaces/primeng.git (fetch)
upstream        disabled (push)


## Update Fork repository

Now that we have the upstream remote, we can update our master branch by merging the upstream branch in our master branch. If we haven’t changed anything and we just want to update the fork repository to the same state as the main repository, we can reset on the upstream branch.

## Conclusion

Today we saw how to keep a fork repository up-to-date by looking first at how to setup an upstream remote, then making sure that we prevent writes to the main repository and finally looking at options to update our fork repository. Hope you liked this post, see you on the next one!

Designed, built and maintained by Kimserey Lam.