Teaching How to push your code into multiple Remote Git repositories

Very quickly Git has become one of the most common ways to maintain and manage source code. It is easy to use, fast, reliable and most modern CI/CD tooling support it. GitHub also makes it easy to anyone who wants to share code, to do it in a free or very inexpensive way. Many companies however, also look for ways in which they can maintain their own private repositories as an enterprise-grade solution, like Developer Cloud Service (DevCS), the one Oracle gives for free with any IaaS or PaaS service.

In this blog I am going to show you how to push your code into any number of remote Git repositories. For example, you can have your private repository in DevCS and choose to also publish them into another GitHub remote repository (public or private) in GitHub.

This is the high-level idea:

  1. Let’s create a new Git repo in DevCS
  2. Let’s create a repo in GitHub
  3. Let’s clone DevCS repo locally on my laptop
  4. Let’s push the code to DevCS Git repo
  5. Let’s push the code to GitHub repo.

Before we start

In this blog I assume that you already have:

Let’s create DevCS and GitHub repositories

In this section we are going to create a GIT repo in DevCS.

  • Log into your DevCS account and project
  • Click on Project on the left menu and then click on “New Repository
  • Give it a name, Description and select “Initialize repository with README file”. Then, click “Create

  • Still under Project tab, search for your new repo on the right and make a note of its HTTPS URL. We’ll use it later when cloning it locally.

  • Now, go to your GitHub account and create a new repository as well. Since we want to push the content from DevCS Git repo into this GitHub repo, make sure you don’t initialise it with any README file. Also, you can keep it public if you want.

  • Great, we now have two Git repos. Let’s move on to the next section to go through the meat and potatoes of this blog, which is how to Git push into 2 or more repos.

Configuring Git client to pushing your code into DevCS Git repo

Now, we are going to Git clone our DevCS repo locally and then configure it to be able to easily Git push into both remote repositories.

  • First let’s clone the DevCS Git repo. For this, in your laptop, open a terminal window and type Git clone [HTTPS URL] – Where HTTPS_URL is the one that you copied previously.

  • Move to your repo folder that you just cloned. Add a new file and save it. This is just to verify that we can locally edit and push changes to both remote Git repos.

  • Locally Git add and commit your changes.

    git add -A

    git commit -m “Adding my current changes into version control repo”

    git status

  • Now, your local master branch is ahead of the current remote master branch in DevCS.
  • Before we commit anything, let’s see what are the remote repositories that we have currently available. For this, type:

    git remote -v

Note: Since we used Git clone to configure our current Git repo, the only remote GIT server that is configured is the source, i.e. DevCS. This means that if we Git push right now, automatically we are going to be pushing to origin, that happens to be DevCS. This is quick an easy when we only have one Git repo, but what in this case we are going to end up having 2 remote Git servers (DevCS and GitHub), so we don’t want to by mistake push into the wrong server. For this reason, I would recommend that you create new Git remote server references with better names and avoid the default ‘origin’ that has made me get it wrong so many times.

  • We can now get rid of the default origin name to avoid confusions in the future.

    git remote remove origin

  • You should now only have remote reference with devcs name

Add GitHub as a second remote Git server

Now, it’s time to add the second remote Git server, but this time pointing to your GitHub.

So far, we have been using HTTPS to communicate with Git (fetch/push). I personally prefer SSH, as it gives me the opportunity to use my SSH key instead of having to be leaving username and passwords here and there. In order to use SSH instead of HTTPS, you need to setup your SSH Keys (see here if you need help).

  • First, we need to configure your local environment. Go to $HOME/.ssh (e.g. within Linux it is ~/.ssh and for Windows it is under C:\Users\[YOU]\.ssh)
  • Copy your Private Key into your $HOME/.ssh directory
  • Create a file called config and enter the following configuration:

    host github.com

    HostName github.com

    IdentityFile ~/.ssh/[my_private_key_name]

    User git

    Where [my_private_key_name] is the name of your Private Key that you copied previously.

  • Go to GitHub and click on your profile picture at the top right > Settings > SSH and GPG keys – Then click on New SSH key. Give it a title and paste the content of your ‘Public’ key.

  • Great, now go back to your new repo in GitHub and copy the URL, but this time make sure you select SSH

  • Go back to your terminal in your laptop and add a second remote server, but this time using the GitHub SSH URL. Call it github this time.

    git remote add [name] [SSH_URL]

    e.g. git remote add github git@github.com:solutionsanz/myTestRepo.git

  • Show all active remote servers, you should have 2 sets, one fetch/push for devcs and the other for github

    git remote -v

  • Now, we are ready to push out our local changes into both remote repos. Let’s do it by explicitly mentioning the name of the remote repo, as well as the branch we are referring to.

    git push devcs master

    git push github master

Notice that for github, you are using SSH and for DevCS you are using SSH! I recommend always to use a passphrase in your private key though.

  • Now, go to each console and make sure that the push worked by finding the file that you originally created and committed.

I hope you found this blog useful. If you have any question or comment, feel free to contact me directly at https://www.linkedin.com/in/citurria/

Thanks for your time.

Author: Carlos Rodriguez Iturria

I am extremely passionate about people, technology and the most effective ways to connect the two by sharing my knowledge and experience. Working collaboratively with customers and partners inspires and excites me, especially when the outcome is noticeable valuable to a business and results in true innovation. I enjoy learning and teaching, as I recognise that this is a critical aspect of remaining at the forefront of technology in the modern era. Over the past 10+ years, I have developed and defined solutions that are reliable, secure and scalable, working closely with a diverse range of stakeholders. I enjoy leading engagements and am very active in the technical communities – both internal and external. I have stood out as a noticeable mentor running technology events across major cities in Australia and New Zealand, including various technology areas such as, Enterprise Integrations, API Management, Cloud Integration, IaaS and PaaS adoption, DevOps, Continuous Integration, Continuous Automation among others. In recent years, I have shaped my role and directed my capabilities towards educating and architecting benefits for customers using Oracle and AWS Cloud technologies. I get especially excited when I am able to position both as a way to exceed my customers’ expectations. I hold a bachelor degree in Computer Science and certifications in Oracle and AWS Solutions Architecture.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s