GitHub
Fork
Repository
Sync
Update

How do I update or sync a forked repository on GitHub?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Forking repositories on GitHub allows developers to contribute to projects by creating their own copies of existing repositories. However, as the original repository (often referred to as the "upstream" repository) evolves, it's crucial to keep your forked repository up-to-date with these changes to avoid conflicts and ensure compatibility. This guide describes how to update or sync a forked repository on GitHub.

Steps to Update or Sync a Forked Repository

  1. Clone the Forked Repository
    First, make sure you have a local copy of your forked repository. If you haven't cloned it yet, run the following command in your terminal or command prompt:
bash
   git clone https://github.com/YOUR_USERNAME/YOUR_FORKED_REPO.git
   cd YOUR_FORKED_REPO

Replace YOUR_USERNAME with your GitHub username and YOUR_FORKED_REPO with the name of your forked repository.

  1. Add Upstream Remote
    Next, you’ll need to add the original repository (upstream) as a remote reference. This allows you to pull in changes from the upstream repository.
bash
   git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git

Replace ORIGINAL_OWNER with the username or organization name of the original repository owner, and ORIGINAL_REPO with the name of the original repository.

  1. Fetch from Upstream
    To update your local copy with the latest changes from the upstream repository:
bash
   git fetch upstream

This command fetches all the branches and their commits from the upstream repository. Your local repository state now reflects any updates made to the upstream repository.

  1. Merge Upstream Changes
    After fetching, you need to merge the changes into your main branch (usually main or master).
bash
   git checkout main  # Switch to your main branch
   git merge upstream/main

This merges changes from the upstream main branch into your local main branch. In case of merge conflicts, you'll need to resolve them manually.

  1. Push Changes to Your Fork
    Finally, push the merged changes back to your GitHub forked repository to update it.
bash
   git push origin main

This ensures your forked repository on GitHub is in sync with the upstream repository.

Alternative Method: Rebasing

Instead of merging, you can also rebase your branch with upstream changes:

bash
1git checkout main
2git fetch upstream
3git rebase upstream/main
4git push origin main --force

Rebasing can provide a cleaner project history by applying your changes on top of the upstream changes. Note that force-pushing is required after rebasing, but it can rewrite commit history, so use it with caution.

Key Considerations

  • Merge vs. Rebase: Merging preserves complete history and is generally safer, while rebasing offers a cleaner project history but requires careful use to prevent losing changes.
  • Resolve Conflicts: If there are conflicting changes between your fork and the upstream repository, Git will prompt you to resolve these conflicts manually.
  • Regular Updates: Frequently syncing your fork with the upstream repository helps minimize conflicts and keeps your forked version aligned with the latest changes.

Summary Table of Steps

StepDescription
Clonegit clone https://github.com/YOUR_USERNAME/YOUR_FORKED_REPO.git cd YOUR_FORKED_REPO
Add Upstreamgit remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPO.git
Fetch Upstreamgit fetch upstream
Merge/Rebase Changesgit merge upstream/main or git rebase upstream/main
Push to Forkgit push origin main

Syncing your fork with the upstream repository is a crucial aspect of collaborative software development. By regularly updating your fork, you ensure that your contributions integrate smoothly with the ongoing project. Remember to weigh the pros and cons of using merge versus rebase and carefully resolve any conflicts during the update process.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions