Mastering GitHub: How to Keep Your Forked Repository in Sync with the Original

Jay
3 min readMay 16, 2023

Don’t let your forked repository fall behind! Learn the step-by-step process to effortlessly sync your fork with the latest changes from the original repository.

Keep Your Forked Repository in Sync with the Original

Scenario: Let’s dive into a real-world scenario. Imagine you encountered an issue a few months ago while using any open-source tool library (in my case, it’s “ cfn-lintCloudFormation Linter”). Being an open-source enthusiast, you decided to contribute by fixing the problem and even submitted a pull request(my PR link) to the original repository. However, as time passed, you realized your forked repository is missing out on the recent changes. Now, you’re eager to raise another pull request but need to update your forked repo. Let’s explore the steps you can take to achieve this.

Section 1: Setting Up the Upstream Repository

To begin, we need to set up the upstream repository. This will allow us to track changes made to the original repository. Follow these steps:

  1. Open the terminal or command prompt on your local machine.
  2. Navigate to the directory where you have cloned your forked repository.
  3. Add a remote upstream repository pointing to the original repository. This step is necessary to keep track of the changes made in the original repository. Use the following command:
#Replace <original_repo_url> with the URL of the original repository.
git remote add upstream <original_repo_url>

By setting up the upstream remote, you establish a connection to the original repository, making it easier to track changes.

Section 2: Fetching and Merging Changes

Now that we have the upstream repository set up, let’s fetch the latest changes and merge them into our forked repository. Follow these steps:

4. Fetch the latest changes from the upstream repository:

# fetch only branches
git fetch upstream
# fetch branches and tags as wll
git fetch upstream --tags

This command retrieves the latest commits and branches from the original repository.

5. Switch to the branch you want to update with the changes from the original repository:

#Replace <branch_name> with the name of the branch, you want to update.
git checkout <branch_name>

6. Merge the changes from the upstream repository into your local branch:

# Replace <branch_name> with the name of the branch in the upstream repository you want to merge.
git merge upstream/<branch_name>

This command merges the changes from the corresponding branch in the upstream repository into your local branch.

Repeat the 5th and 6th steps for each branch you want to update. By fetching and merging the changes, you’ll ensure that your forked repository stays in sync with the original.

Bonus: If you want to update all the branches then check out each branch in your forked repository and merge the corresponding branch from the upstream repository. This can be done using a loop in your shell. Here’s an example using Bash:

# Iterate over all branches in the upstream repository
for branch in $(git branch -r | grep -v '\->'); do
# Extract the branch name
branch_name=${branch##*/}
# Check out the branch in your forked repository
git checkout $branch_name
# Merge the corresponding branch from the upstream repository
git merge upstream/$branch_name
done

Section 3: Pushing Changes to Your Forked Repository

7. Push the merged changes to your forked repository:

# single branch push
# Replace <branch_name> with the name of the branch you updated.
git push origin <branch_name>
# all branches
git push --all origin
# Push the tags from the upstream repository to your forked repository:
git push --tags origin

After completing these steps, your forked repository should be up to date with the changes from the original repository. You can then create a pull request if you want to contribute the changes to the original repository.

Congratulations! You now possess the knowledge to keep your forked GitHub repository up to date with the original. By following these simple steps, you’ll ensure your contributions align with the latest developments, empowering you to deliver high-quality fixes and features.

Stay connected for more insightful tutorials on Linux, Cloud, and DevOps by following me.

--

--