2 minute read

Forking a repository is a common practice in Git, especially when contributing to a project or maintaining a separate version. A key challenge, however, is keeping a fork updated with the original repository while managing feature and production branches. This guide outlines the process step by step.

Why Keeping a Fork Updated Matters

When a repository is forked:

  • A personal copy of the project exists on GitHub (origin).
  • The original repository (upstream) continues to receive updates from contributors.
  • Falling behind the upstream repository can lead to merge conflicts and missed improvements.
  • Staying in sync ensures development workflows remain smooth and conflict-free.

Step 1: Add the Original Repository as a Remote

By default, a fork only knows about origin. To fetch updates from the original project, it is added as upstream:

git remote add upstream <original-repo-url>

Verify the remotes:

git remote -v

Expected output:

origin    git@github.com:ourusername/forked-repo.git (fetch)
origin    git@github.com:ourusername/forked-repo.git (push)
upstream  git@github.com:original-owner/original-repo.git (fetch)
upstream  git@github.com:original-owner/original-repo.git (push)

Here, origin is our fork, and upstream is the original repository.

Step 2: Fetch Updates from Upstream

Fetching brings the latest commits from the original repository without changing local branches:

git fetch upstream

Step 3: Update the Feature Branch (latest)

The latest branch is used for feature development. First, switch to this branch:

git checkout latest

Integrate updates from the upstream master:

git merge upstream/master
# or
git rebase upstream/master

This ensures the feature branch has all upstream updates before development and testing.

Step 4: Test Changes in the Feature Branch

All updates and new features should be tested in the latest branch. Conflicts or issues can be resolved here without affecting production.

Step 5: Update the Production Branch (master)

Once the feature branch is tested and verified, merge changes into the production branch:

git checkout master
git merge latest

This updates master with tested changes, ensuring a stable production environment.

Step 6: Push Updates to the Fork

In Git, origin refers to our fork on GitHub, while upstream refers to the original repository. After updating the branches locally, push them to our fork using:

git push origin latest
git push origin master

This ensures that both the feature latest and production master branches are up to date on our fork without affecting the original repository upstream.

  1. Fetch upstream updates before starting work:
git fetch upstream
git checkout latest
git merge upstream/master
  1. Develop and test in latest.
  2. Merge latest into master for production after testing.
  3. Push branches to the fork regularly.

This workflow keeps the fork synchronized, the feature branch current, and the production branch stable.

Conclusion

Maintaining a fork in sync with the original repository is essential for smooth Git workflows. By adding an upstream remote, regularly updating the feature branch, testing changes, and carefully merging into the production branch, it is possible to manage development efficiently and avoid conflicts. This approach ensures that production remains stable while development continues without disruption.

Leave a comment