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

One task which is virtually impossible to do properly through the GIT Version Control web interface, is syncing a forked repository. Fortunately, there is a fairly straightforward way to automatically merge the changes via the command line interface.

Let’s say for example, a few days back we created a fork called chriscase/friendika off of the main branch of the repository friendika/friendika, using the GITHub web interface.

Since the time we created the chriscase/friendika fork, there were commits made on the main branch you copied the fork from. We need to get everything back in sync now; this way we’ll be working on the latest code.

To do this synchronization, let’s log into the server we’re going to be doing our work on and clone the fork locally; 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 repository:

[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 repository with the master friendika/friendika repository we want to pull changes from. Then we will fetch the code from the master repository.

[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 master repository at friendika/friendia and we’ve fetched the code from the master repository; we need to sync our chriscase/friendika fork with the master repository.

We do the actual merge by running the merge command:

[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 conflicts, which there shouldn’t be since we haven’t checked in any changes yet, the merge should take place without incident.

Next we need to push the merged version of our code back to our chriscase/friendika fork. This is done with the following command:

[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 coding on the project!

Further Reading:

5 comments

  1. FYI, here is what I do to merge pull requests, and I think it could easily work for any remote repository. Let’s call the other person’s repository “repo”

    git checkout -b repo master
    # after you’ve done it once you could just
    # git checkout repo
    git pull http://github/something/repo master
    git checkout master
    git merge repo
    # repeat as often as needed while you’re
    # working on stuff on your master branch
    # (or another branch you’ve created)
    # when you’re ready to push your stuff
    git push origin master

    If you just change the name repo and the location of the remote repository this same formula could be used to sync with anybody else’s repository anywhere

  2. Pingback: git 배우기
  3. Thanks for this tutorial. Was searching long time to find a working solution to sync my forks with the master repository. This one is the first which is easy enough for me to understand.
    One question. If there are conflicts between the master repository and the fork, how is it shown?

  4. Excellent tutorial exactly what I wanted to do. You need to get some ‘share this’ links to twitter, fb, etc.
    Thanks again
    Lewismc


  5. lewismc:

    Excel­lent tuto­r­ial exactly what I wanted to do. You need to get some ‘share this’ links to twit­ter, fb, etc.
    Thanks again
    Lewismc

    Hey Lewismc,

    Thanks for the suggestion! I have updated the theme accordingly.

Leave a comment

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