Keeping your forked repository in sync with its upstream counterpart is essential for ensuring your contributions are built on the most recent developments. This guide walks you through the process using git rebase, a method preferred by many for its ability to create a clean, linear project history. Let's dive in.

Step 1: Linking the Original Repository

Start by connecting your fork to the original repository, and setting it up as a new remote source. This connection is crucial for syncing changes. Name this remote “upstream” to differentiate it from your fork:

git remote add upstream https://github.com/path/to/the/original-repo.git

Step 2: Fetching the Latest Changes

With the upstream set, it’s time to fetch the latest changes. This action updates your local copy with all branches from the upstream repository, ensuring you’re working with the most current data:

git fetch upstream

Step 3: Rebasing Your Local Branch

Now, bring your local branch up to speed by rebasing it onto the upstream’s main branch. Rebasing rewrites your branch’s changes on top of the upstream branch, leading to a streamlined project history:

git rebase upstream/main

Step 4: Propagating Your Changes

After rebasing, push your updated branch back to your fork. To avoid accidentally overwriting un-synced work, use the --force-with-lease option. This command ensures your push only goes through if your local repository is fully up-to-date with your fork:

git push --force-with-lease origin main

By following these steps, you align your fork with the upstream repository while preserving a tidy history. This approach not only facilitates easier contributions but also simplifies maintenance and collaboration.