Git Add Another Remote to an Existing Repository: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 59: Line 59:


  [branch "master"]
  [branch "master"]
'''remote''' = '''origin'''
      '''remote''' = '''origin'''
merge = refs/heads/master
      merge = refs/heads/master

Revision as of 22:10, 11 December 2017

Internal

Overview

This article describes the procedure of adding a second remote to an existing repository. A practical situation when we would need something like this is when we developed an an application in an internal repository - for example a Gogs instance deployed within an OpenShift cluster - and we want to share the example in GitHub, while maintaining the internal repository fully operable. We start from a local repository fully synchronized with the Gogs repository:

git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
git remote -v show
origin	https://gogs-cicd.apps.openshift.novaordis.io/gogs/novaordis-session-servlet.git (fetch)
origin	https://gogs-cicd.apps.openshift.novaordis.io/gogs/novaordis-session-servlet.git (push)

For the sake of simplicity, we will use just one branch ("master"), which will have a corresponding "master" [Git_Concepts#Remote-Tracking_Branch|remote-tracking branch]] in each remote repository.

Procedure

Create the Second Repository

Use GitHub's UI. For consistency, use the same repository name, though it is not necessary.

Add the Second Remote

From the local repository, add the GitHub remote:

git remote add github git@github.com:NovaOrdis/novaordis-session-servlet.git

The remote is immediately added:

git remote -v show
github	git@github.com:NovaOrdis/novaordis-session-servlet.git (fetch)
github	git@github.com:NovaOrdis/novaordis-session-servlet.git (push)
origin	https://gogs-cicd.apps.openshift.novaordis.io/gogs/novaordis-session-servlet.git (fetch)
origin	https://gogs-cicd.apps.openshift.novaordis.io/gogs/novaordis-session-servlet.git (push)

and .git/config is updated accordingly

...
[remote "github"]
       url = git@github.com:NovaOrdis/novaordis-session-servlet.git
       fetch = +refs/heads/*:refs/remotes/github/*
...

The second remote-tracking branch is automatically setup:

 git branch -a
 * master
   remotes/github/master
   remotes/origin/master

State

At this point, the master branch's default remote is origin:/master:

[branch "master"]
     remote = origin
     merge = refs/heads/master