Get the latest posts delivered right to your inbox.

or subscribe via RSS with Feedly!

How to sync a branch with the original repository in Github

There are not an option to sync the forks with the original repository in the GitHub website so you can sync the branch in 2 ways, one is using the desktop app, and another is using the terminal which I will explain in this article.


In the forked repository, we need to add the original GitHub repository with this command:

git remote add upstream

upstream is just a nickname, you can use the name that you want, and of course you also need to change the repository URL with the original repository.

Now we need to fetch all the branches of the original repository into remote-tracking branches, in this case, we will sync upstream/master:

git fetch upstream

Make sure that you're on your master branch:

git checkout master

The next command will overwrite your master branch so take care if you have local commits that are not in the original repository because it will be erased:

git rebase upstream/master

If you don't want to rewrite the history of your master branch, because other people may have cloned it or you just want to keep it, then you should replace the last command with:

git merge upstream/master

If you have changes that are different from the original repository you will need an extra work, so for making further pull requests that are as clean as possible, it's probably better to rebase.

If you've rebased your branch onto upstream/master you may need to force the push in order to push it to your own forked repository on GitHub, with this command:

git push -f origin master

You only need to use the -f the first time after you've rebased.