• Home
  • /
  • Blog
  • /
  • How to Quickly Setup and Use Two or More Git Repositories for One Single Project

How to Quickly Setup and Use Two or More Git Repositories for One Single Project

Sometimes we need to sync our local code base with  2 or more Git remote repositories. For example, if your source code is on the Github for issues tracking and also pushed to Heroku for production.
Here is the step by step instruction on setting up a project to sync with 2 or more remote git repositories (on Github and Heroku).

What we have for this tutorial:

  • Git repository on Github: /my-company/my-project
  • Git repository on Heroku, where the project is also named “my-project
  • The need to work with both repositories at the same time
Important: before you make any changes, please make a backup of the local folder with your project.

1. First clone your project from one of these repositories like this:

2. Clone the repository from Github:

git clone git@github.com:my-company/my-project.git

3. Open the folder with the cloned project:

cd my-project
4. Now we will manually edit Git repository’s configuration file where we will add the second repository as a new remote source.
Open .git/config file. If you can’t find .git subfolder then make sure you have enabled showing of hidden subfolders on your system.
This file (.git/config) will look like this in your editor. This is the content of the config file automatically generated once you’ve cloned it from Github.
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote "origin"]
url = git@github.com:my-company/my-project.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
5. As you see, when you have one single repository, it is called “origin” by default. If we have two or more remote repositories then we should name each of them using unique(!) names. Also, do not hesitate to add empty lines for more readable code, empty lines will be ignored by git so no worries.
So, now rename “origin” into “github” and  config will look like this:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote “github“]
url = git@github.com:my-company/my-project.git
fetch = +refs/heads/*:refs/remotes/github/*
[branch “master”]
remote = github
merge = refs/heads/master
From now on if you want to push to this repository you will have to write “git push github master” instead of just “git push“. To pull you should type “git pull github master” as well.
6. Now it is time to add the second repository into the config. Duplicate the code starting from [remote.. and up to the end so you will get this code:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote “github”]
url = git@github.com:my-company/my-project.git
fetch = +refs/heads/*:refs/remotes/github/*
[branch “master”]
remote = github
merge = refs/heads/master
[remote “github”]
url = git@github.com:my-company/my-project.git
fetch = +refs/heads/*:refs/remotes/github/*
[branch “master”]
remote = github
merge = refs/heads/master
7. Now rename “github” into “heroku” in the copied section so you will get this:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote “github”]
url = git@github.com:my-company/my-project.git
fetch = +refs/heads/*:refs/remotes/github/*
[branch “master”]
remote = github
merge = refs/heads/master
[remote “heroku“]
url = git@github.com:my-company/my-project.git
fetch = +refs/heads/*:refs/remotes/heroku/*
[branch “master”]
remote = heroku
merge = refs/heads/master
8. Also now change “url” in the “heroku” section to the url for heroku’s git repository accordingly:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
[remote “github”]
url = git@github.com:my-company/my-project.git
fetch = +refs/heads/*:refs/remotes/github/*
[branch “master”]
remote = github
merge = refs/heads/master
[remote “heroku“]
url = https://git.heroku.com/my-project.git
fetch = +refs/heads/*:refs/remotes/heroku/*
[branch “master”]
remote = heroku
merge = refs/heads/master
9. Congratulations! Now you may pull from and push to both git repositories like this:
To push:
git push heroku master
git push github master
To pull:
git pull heroku master
git pull github master
The usual workflow when working with the code synced to two or more repositories looks like this:
  • make changes in the code of the project;
  • use git add command to add changed/updated files as usual;
  • make a commit using git commit command as usual;
  • push code changes to the first repository: git push github master ;
  • then push code changes to the second repository: git push heroku master.

 

About the Author

Eugene Author
Eugene Mironichev

Eugene is our featured author. He is an expert in software products and team management. In 2017, he published a book about remote work and management of remote teams. He writes articles for Forbes and other business magazines and appears as a speaker at tech and business conferences.

 


Try ByteScout SQL Trainer - Learn SQL in this free online simulator!

PDF.co - Free Online Data Extraction Tools with API e-signatures, invoices, pdf, barcodes, spreadsheets, documents tranformations. No credit card required.

Try ByteScout SDK for Software Developers - Offline Data Extraction, Barcodes, Spreadsheets Components. Free 60 Day trial.
prev
next