Legacy Archive (2014): Originally written in Chinese on doubixie.com and translated into English for this edition. All archive posts · Home

Syncing a Forked Git Repo with Upstream Changes

Original Chinese title: git获取fork之后的项目最新代码

The Problem

  • When you fork a project on GitHub and work on it locally, updates to the upstream repo aren’t synced automatically—you have to merge them manually.

    I hadn’t noticed this before and always assumed forked repos stayed updated on their own. For the longest time I thought every project I forked had simply gone dead.

Solution

Option 1: GUI

  • Change the Primary remote repository URL to the upstream project’s address.

    Take Master Sanqingshui's nodePPT (an online presentation system written in Node) as an example: after forking it to your GitHub and cloning it locally, when you notice the original project has updates:
    1. Change the origin or PR repository URL to the upstream repo.
    2. Pull down the latest changes, switch the URL back to your own repo, and push.
    3. Done.

Option 2: Command Line

  • Personally, I much prefer this method:

    >git remote add <alias> <url>
    >git fetch <alias>
    >git merge <alias>/master
    
    For example, with NodePPT:
    >git remote add NodePPT https://github.com/ksky521/nodePPT.git
    >git fetch NodePPT
    >git merge NodePPT/master
    >git commit
    >git push
  • A few handy commands:

    git remove -v  # View all remote sources so you can pick the right one.
    git branch -a  # View all branches available to merge.

That’s About It.