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 inter­face.

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 inter­face.

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 server 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 ~/friendikademo.openmindspace.org/, which I use as my test/dev area.

This is how we pull in the files from the github repos­i­to­ry:

[bunda]$ cd friendikademo.openmindspace.org
[bunda]$ git clone https://chriscase@github.com/chriscase/friendika.git .
Cloning into ....
Password:
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 repos­i­to­ry.

[bunda]$ git remote add upstream git://github.com/friendika/friendika.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://github.com/friendika/friendika
* [new branch]      2.1-branch -> upstream/2.1-branch
* [new branch]      master     -> upstream/master
From git://github.com/friendika/friendika
* [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 repos­i­to­ry.

We do the actu­al merge by run­ning the merge com­mand:

[bunda]$ git merge upstream/master
Updating a05b2b4..5e02519
Fast-forward
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 shouldn’t be since we haven’t checked in any changes yet, the merge should take place with­out inci­dent.

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 com­mand:

[bunda]$ git push origin master
Password:
Total 0 (delta 0), reused 0 (delta 0)
To https://chriscase@github.com/chriscase/friendika.git
a05b2b4..5e02519  master -> master

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

Fur­ther Read­ing:

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 person’s repos­i­to­ry “repo”

    git check­out –b repo mas­ter
    # after you’ve done it once you could just
    # git check­out repo
    git pull http://github/something/repo mas­ter
    git check­out mas­ter
    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 cre­at­ed)
    # when you’re ready to push your stuff
    git push orig­in mas­ter

    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 any­where

  2. Pingback: git 배우기
  3. Thanks for this tuto­ri­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 under­stand.
    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­ri­al exact­ly what I want­ed to do. You need to get some ‘share this’ links to twit­ter, fb, etc.
    Thanks again
    Lewismc


  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
    Lewismc 

    Hey Lewismc,

    Thanks for the sug­ges­tion! I have updat­ed the the­me accord­ing­ly.

Leave a Reply

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