How to Update a Forked Repository from Upstream

Updated . Posted . Visible to the public.

Scenario

2 to 3 remote repositories in GitHub and 2 local repositories:

  1. [GitHub] Go to an owner repository (eg OpenMage).
  2. [GitHub] Fork owner's repository (from item 1).
  3. [GitHub or BitBucket] Our application with custom code.
  4. [Local] Mirror of fork in GitHub, 2 branches: upstream remote to item1, and origin remote to item 2. This local repo is for code contribution to owner.
  5. [Local] Application, 3 or more branches: upstream remote to item1, origin remote to item 2, and a master branch remote to item 3. Master contains our custom code specific to our application.

To update our fork (items 2 & 4), we update our local repositories by fetching and pulling from upstream, then push to remote item 2.

To update our application (items 3 and 5), in local, we fetch and pull from upstream, switch to master and merge it with upstream. Finally, we push master to remote item 3.

Prerequisites

Clone a local copy of the forked repository in GitHub. With TortoiseGit:

Image

Update in GitBash Show archive.org snapshot

Only 3 steps to update the forked repo:

  1. Launch gitBash in the root folder
  2. Pull from upstream (owner of the repo)
  3. Push to origin (forked repo)
kiat@win10 MINGW64 /d/Work/Oro/platform-application (master)
$ git pull https://github.com/oroinc/platform-application.git master
remote: Enumerating objects: 36, done.
remote: Counting objects: 100% (36/36), done.
remote: Compressing objects: 100% (15/15), done.
remote: Total 45 (delta 24), reused 33 (delta 21), pack-reused 9
Unpacking objects: 100% (45/45), done.
From https://github.com/oroinc/platform-application
 * branch              master     -> FETCH_HEAD
Updating e27cd733..046f118f
Fast-forward
 composer.json           |    3 +-
 config/config_dev.yml   |   26 +-
 config/config_prod.yml  |   22 +
 dev.json                |    1 +
 dev.lock                | 1380 ++++++++++++++++++++++++++++++++---------------
 public/.htaccess        |    3 +
 var/OroRequirements.php |   10 +-
 7 files changed, 992 insertions(+), 453 deletions(-)

kiat@win10 MINGW64 /d/Work/Oro/platform-application (master)
$ git push origin master
Counting objects: 45, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (45/45), done.
Writing objects: 100% (45/45), 29.02 KiB | 803.00 KiB/s, done.
Total 45 (delta 25), reused 0 (delta 0)
remote: Resolving deltas: 100% (25/25), completed with 10 local objects.
To https://github.com/kiatng/platform-application.git
   e27cd733..046f118f  master -> master

Alternate Method using TortoiseGit

In the local repo which was cloned earlier, create a upstream remote pointing to the owner's repo:

Image

Now, to update the forked repo:

  1. [TortoiseGit] Switch to branch master
  2. [TortoiseGit] Pull from remote upstream branch master
  3. [TortoiseGit] Push to remote origin branch master

Update Local Repo from Upstream

Prerequisites

First create a local branch upstream_master to hold the latest branch from upstream. Then do a hard reset:

$ git branch upstream_master
$ git checkout upstream_master
$ git fetch upstream
$ git reset --hard upstream/1.9.4.x #default branch

Now, upstream_master has the latest updates. Before we can merge this to local master, we need to remove unwanted files. We remove the files manually and then commit changes to upstream_master.

Update by master merging upstream_master into it

The forked repo is for contributing to the owner's repo. For production or actual running the application, we need to have a separate local and remote repos running locally or in a server. There will be time when we need to update our application with the latest version from the owner. I created a branch upstream_master to hold the latest version from the owner. To update the local master branch, do:

  1. [TortoiseGit] Switch to branch upstream_master
  2. [TortoiseGit] Pull from remote upstream branch master
  3. [TortoiseGit] Switch to branch master
  4. [TortoiseGit] Merge from branch upstream_master to master
    • git merge --allow-unrelated-histories upstream_master
  5. [TortoiseGit] If there are conflicts, resolve them by using the files from upstream_master, then commit the changes into master
  6. [TortoiseGit] Push to remote origin
kiatng
Last edit
kiatng
Posted by kiatng to Git (2019-04-11 02:04)