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:
- Let’s create a new Git repo in DevCS
- Let’s create a repo in GitHub
- Let’s clone DevCS repo locally on my laptop
- Let’s push the code to DevCS Git repo
- Let’s push the code to GitHub repo.
Before we start
In this blog I assume that you already have:
- A GitHub account, if not open one, it’s free. Go to: https://www.Github.com
- A DevCS account, if not request a trial, they are free. Go to: https://cloud.oracle.com/tryit
- You have created your DevCS project to work on. If not, see how here.
- You have install Git locally on your laptop. If not, follow this quick guide.
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”
- 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.
Create a new Git remote server reference to point to DevCS:
git remote add [name] [GIT_URL]
e.g. git remote devcs https://email@example.com/the-castle/scm/myTestRepo.Git
Now show the remote servers again:
git remote -v
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:
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 firstname.lastname@example.org: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.