I recently had the opportunity of contributing code to the upcoming default theme of WordPress — Twenty Nineteen on GitHub. The project was developing at a quite fast pace. It was receiving an average of 20 to 30 commits and PRs — pull requests — on a daily basis.

To create pull requests of my own, I had to fork the repository to my account. As soon as I sent my first PR, the master branch of my cloned repo was already a few commits behind the master branch of the original repository.

Now, before submitting my next PR, I had to sync both of these branches so that my future pull requests do not have any conflicts with the original repository. Here’s how I was able to update my forked repo with the original one.

🔰 Clone the Repository

First of all, you should clone the forked repository to your local computer. Open the terminal, and use the following command to clone the repository.

git clone https://github.com/YOUR_USERNAME/YOUR_REPOSITORY.git

Now change the current working directory in the terminal to the cloned repository using this command.


➕ Add a New Remote

Now, you need to check the remotes of your Git repository. Enter the following command in the terminal and press enter.

git remote -v

As you can see in the screenshot, origin is the only remote configured for the repository. You need to add another remote called upstream to the repo using the following command.

git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git

In your case, you need to replace the Git URL in the command with the original URL of your forked git repository.

Now once again check the remotes of the repository by using the command you used above — git remote -v. You will now see a new set of remotes for your git repository.

♻️ Sync the Fork

In this last step, you will sync your forked repository with the original. This process is the same as pulling changes from your git repository, i.e., you need to fetch and pull the changes from the remote of your git repo, but the name of the remote is going to be different.

As you need to pull the changes from the upstream remote now, you will enter the following commands in the terminal.

git fetch upstream git pull upstream master

These commands will fetch and pull the changes from the upstream remote and will merge these changes with the master branch of your forked repository. Now all you need to do is push these changes to your fork. Use the following the command in the terminal for this purpose.

git push origin master

As you can see, this command will push the changes to the origin remote of the repository, i.e., the forked one.

🎁 Bonus

If you’re a zsh shell user like myself, you can add the following alias and function in your .zshrc file to sync your fork within seconds.

How to Update a Forked Git Repository — GIF

Cheers ✌️