DEV Community

Abhishek Keshri
Abhishek Keshri

Posted on • Originally published at 2kabhishek.blogspot.com on

Sync Forked Repository With Upstream 🔁✔️

Here's the situation, You have forked a repository on GitHub, you have made your changes and sent a pull request and that request has been merged.

Now you want to make further changes, but the repository mentions that you are X commits ahead or/and X commits behind upstream repository.

So how do you sync it?

You can delete your fork and fork the original repository again, but that is a very tedious thing to do.

Here's another way to do this:

  1. Clone your fork
git clone https://github.com/yourname/repo.git
cd yourname/repo
Enter fullscreen mode Exit fullscreen mode
  1. Add upstream remote
git remote add upstream https://github.com/original-author/repo.git
git fetch upstream
Enter fullscreen mode Exit fullscreen mode
  1. Ignore all changes in fork and sync with upstream
git reset --hard upstream/master
git push -f origin master
Enter fullscreen mode Exit fullscreen mode

Only do this if you have no changes on your fork that you will like to keep.

This will set your fork's master to be the same as upstream's master.

Now you are free to make any changes you wish to make.

If you just want to get the changes from upstream you can do

git pull upstream master 
git push origin master
Enter fullscreen mode Exit fullscreen mode

Just a word of caution: Always be extra careful when using the -f or --force option.

Thank you for reading, hope you found this useful, see you in the next one.

Discussion (0)