Syncing a Forked git Repository With a Master Repository’s Changes

One task which is vir­tu­al­ly impos­si­ble to do prop­er­ly through the GIT Ver­sion Con­trol web inter­face, is sync­ing a forked repos­i­to­ry. For­tu­nate­ly, there is a fair­ly straight­for­ward way to auto­mat­i­cal­ly merge the changes via the com­mand line interface.

Let’s say for exam­ple, a few days back we cre­at­ed a fork called chriscase/friendika off of the main branch of the repos­i­to­ry friendika/friendika, using the GITHub web interface.

Since the time we cre­at­ed the chriscase/friendika fork, there were com­mits made on the main branch you copied the fork from. We need to get every­thing back in sync now; this way we’ll be work­ing on the lat­est code.

To do this syn­chro­niza­tion, let’s log into the serv­er we’re going to be doing our work on and clone the fork local­ly; this way we have our local copy to work with. I’m going to be doing my work in ~/, which I use as my test/dev area.

This is how we pull in the files from the github repository:

[bunda]$ cd
[bunda]$ git clone .
Cloning into ....
remote: Counting objects: 9907, done.
remote: Compressing objects: 100% (4047/4047), done.
remote: Total 9907 (delta 6324), reused 8563 (delta 5320)
Receiving objects: 100% (9907/9907), 5.20 MiB | 1.00 MiB/s, done.
Resolving deltas: 100% (6324/6324), done.

Now we’re going to link our local repos­i­to­ry with the mas­ter friendika/friendika repos­i­to­ry we want to pull changes from. Then we will fetch the code from the mas­ter repository.

[bunda]$ git remote add upstream git://
[bunda]$ git fetch upstream
remote: Counting objects: 207, done.
remote: Compressing objects: 100% (151/151), done.
remote: Total 157 (delta 123), reused 0 (delta 0)
Receiving objects: 100% (157/157), 21.15 KiB, done.
Resolving deltas: 100% (123/123), completed with 45 local objects.
From git://
* [new branch]      2.1-branch -> upstream/2.1-branch
* [new branch]      master     -> upstream/master
From git://
* [new tag]         2.1-stable -> 2.1-stable

Now that we’ve got a local copy of my chriscase/friendika fork, we’ve linked up to the mas­ter repos­i­to­ry at friendika/friendia and we’ve fetched the code from the mas­ter repos­i­to­ry; we need to sync our chriscase/friendika fork with the mas­ter repository.

We do the actu­al merge by run­ning the merge command:

[bunda]$ git merge upstream/master
Updating a05b2b4..5e02519
addon/facebook/LICENSE                             |  662 --------------------
addon/facebook/facebook.php                        |  213 ++++++-
addon/statusnet/statusnet.php                      |   27 +-
addon/twitter/twitter.php                          |   89 ++-
boot.php                                           |   36 +-
database.sql                                       |    4 +-
include/acl_selectors.php                          |   22 +
include/bbcode.php                                 |   10 +-
include/html2bbcode.php                            |   53 ++-
include/items.php                                  |    6 +-
include/notifier.php                               |   18 +-
include/oembed.php                                 |    5 +-
index.php                                          |    9 +-
mod/cb.php                                         |   24 +
mod/follow.php                                     |   27 +-
mod/item.php                                       |   26 +-
mod/network.php                                    |    5 +-
mod/profile.php                                    |    6 +-
mod/pubsub.php                                     |    4 +-
mod/salmon.php                                     |    2 +-
.../tiny_mce/plugins/bbcode/editor_plugin_src.js   |    2 +-
update.php                                         |    7 +
util/strings.php                                   |   44 +-
util/typo.php                                      |    2 +-
view/de/jot-header.tpl                             |    6 +-
view/de/jot.tpl                                    |    6 +-
view/en/jot-header.tpl                             |    6 +-
view/en/jot.tpl                                    |    6 +-
view/fr/jot-header.tpl                             |    7 +-
view/fr/jot.tpl                                    |    7 +-
view/it/jot-header.tpl                             |    6 +-
view/it/jot.tpl                                    |    7 +-
view/theme/duepuntozero/ff-16.jpg                  |  Bin 0 -> 644 bytes
view/theme/duepuntozero/lock.cur                   |  Bin 0 -> 4286 bytes
view/theme/duepuntozero/login-bg.gif               |  Bin 0 -> 237 bytes
view/theme/duepuntozero/style.css                  |   17 +-
view/theme/loozah/style.css                        |   11 +
37 files changed, 587 insertions(+), 795 deletions(-)
delete mode 100644 addon/facebook/LICENSE
create mode 100644 mod/cb.php
create mode 100644 view/theme/duepuntozero/ff-16.jpg
create mode 100755 view/theme/duepuntozero/lock.cur
create mode 100644 view/theme/duepuntozero/login-bg.gif

If there were no con­flicts, which there should­n’t be since we haven’t checked in any changes yet, the merge should take place with­out incident.

Next we need to push the merged ver­sion of our code back to our chriscase/friendika fork. This is done with the fol­low­ing command:

[bunda]$ git push origin master
Total 0 (delta 0), reused 0 (delta 0)
a05b2b4..5e02519  master -> master

Now that this has been done, you’re ready to start cod­ing on the project!

Fur­ther Reading:

5 thoughts on “Syncing a Forked git Repository With a Master Repository’s Changes”

  1. FYI, here is what I do to merge pull requests, and I think it could eas­i­ly work for any remote repos­i­to­ry. Let’s call the oth­er per­son­’s repos­i­to­ry “repo”

    git check­out ‑b repo master
    # after you’ve done it once you could just
    # git check­out repo
    git pull http://github/something/repo master
    git check­out master
    git merge repo
    # repeat as often as need­ed while you’re
    # work­ing on stuff on your mas­ter branch
    # (or anoth­er branch you’ve created)
    # when you’re ready to push your stuff
    git push ori­gin master

    If you just change the name repo and the loca­tion of the remote repos­i­to­ry this same for­mu­la could be used to sync with any­body else’s repos­i­to­ry anywhere

  2. Pingback: git 배우기
  3. Thanks for this tuto­r­i­al. Was search­ing long time to find a work­ing solu­tion to sync my forks with the mas­ter repos­i­to­ry. This one is the first which is easy enough for me to understand.
    One ques­tion. If there are con­flicts between the mas­ter repos­i­to­ry and the fork, how is it shown?

  4. Excel­lent tuto­r­i­al exact­ly what I want­ed to do. You need to get some ‘share this’ links to twit­ter, fb, etc.
    Thanks again

  5. lewismc:

    Excel­lent tuto­r­ial exact­ly what I want­ed to do. You need to get some ‘share this’ links to twit­ter, fb, etc.
    Thanks again

    Hey Lewismc,

    Thanks for the sug­ges­tion! I have updat­ed the theme accordingly.

Leave a Reply

Your email address will not be published. Required fields are marked *