Git: sync your fork with the original repo

Introduction

When collaborating with github projects, normally, you do your changes in your fork / branch and then push these changes to your origin ( and when you create a PR it should automatically point to original repo master ). Recently I started collaborating with a github project called excalidraw and they use that approach. Even if that sounds a easy thing to do, it is not, specially new developers could struggle to get sync done for first time.

Sync Fork with Original Repo

Best and quicker approach to sync your repo is:

diagram sync fork

Let’s explain the approach:

  1. Configure upstream

    We should have origin: <our fork url> and upstream: <original repo url>. If we don’t have upstream configured then we should add it.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    $ git remote -v

    origin https://github.com/jquintozamora/excalidraw.git (fetch)
    origin https://github.com/jquintozamora/excalidraw.git (push)


    $ git remote add upstream https://github.com/excalidraw/excalidraw.git


    $ git remote -v

    origin https://github.com/jquintozamora/excalidraw.git (fetch)
    origin https://github.com/jquintozamora/excalidraw.git (push)
    upstream https://github.com/excalidraw/excalidraw.git (fetch)
    upstream https://github.com/excalidraw/excalidraw.git (push)

    We should now see both origin and upstream setup properly done.

  2. fetch changes from original repo ( upstream ) ⬇️

    1
    2
    3
    4
    5
    6
    7
    8
    $ git fetch upstream -ap

    remote: Enumerating objects: 286, done.
    remote: Counting objects: 100% (286/286), done.
    remote: Compressing objects: 100% (66/66), done.
    remote: Total 322 (delta 249), reused 241 (delta 220), pack-reused 36
    Receiving objects: 100% (322/322), 125.10 KiB | 1.03 MiB/s, done.
    ...
  3. merge changes from upstream into your fork 🔄

    1
    2
    3
    4
    5
    $ git merge upstream/master

    Updating 6635261..ae1eee1
    Fast-forward
    changes...

    After merge, we will have both repos on sync on our local machine. But, I’d recommend to push changes to have your fork updated on github too.

  4. push to sync changes on github too ⬆️

    1
    2
    3
    4
    5
    $ git push origin master

    Total 0 (delta 0), reused 0 (delta 0)
    To https://github.com/jquintozamora/excalidraw.git
    6635261..ae1eee1 master -> master

🚀🚀 DONE 🚀🚀

I hope it works for you as well, if have any issues, please leave a comment, so we can discuss and try to help!

 

Author: José Quinto
Link: https://blog.josequinto.com/2020/04/04/git-sync-your-fork-with-original-repo/
Copyright Notice: All articles in this blog are licensed under CC BY-SA 4.0 unless stating additionally.