Torbjorn Zetterlund

Wed 10 2015
Image

Git: Pushing your code to two different source code control system using Github client

by bernt & torsten

Depending on who I work with, I get requests sometimes to push code not only to my working GitHub account but also to the client’s account. I’m using the Github client to update from my local machine to the Github repository, what do you do if you need to push code to another source code control system?

I will explain what I did to make this work for me, I like client tools for SaaS service, and I’m not that keen on using the command line to do all the administrative work for Git.

This is my Github client:

Pushing-git-to-two-repositories

All I did was edit the .git/config in the source code folders that are under my Github control; what I did was add another line, URL =, as shown in this picture

git config

All the other config values are set by the Github client on install, I just added one line. This works for me if you love the command line, you could do the following.

You can put the following in the

.git/config

file:

[remote "both"]
 url = url/to/first/remote
 url = url/to/other/remote

You can now push to both URLs using git push both.

If you also want to fetch from them (useful for sync) you may add the following lines in your

.git/config

file:

[remotes]
both = origin, other

Now you can also run

git fetch both

I would not recommend fetching from both, I would use one repro for backup purposes only so a push is enough to the backup repro.

Another way to do this is to follow commands on the command line.

$ git remote -v origin me@original:something.git (fetch) origin me@original:something.git (push) my_other_remote git://somewhere/something.git (fetch) my_other_remote git://somewhere/something.git (push)

You could do:

 git remote set-url --add --push origin git://somewhere/something.git

Then,

git push origin

will push to both repositories. You might want to set up a new remote called both for this, however, to avoid confusion. For example:

 git remote add both me@original:something.git
 git remote set-url --add --push both git://somewhere/something.git

… then:

 git push both

… will try to push to both repositories.

# Push To Multiple Git Repositories

I use two git Repositories

  • GitLab for internal Use and deployment
  • GitHub for public open source code sharing

From the root folder of your project, add both repositories to the remotes:

git remote add origin <GitLab URL>
git remote add copy <GitHUb URL>

Run the git remote -v command to ensure that both remotes were successfully added

Now you are able to perform a push to the selected remote by specifying it in the git push command:

git push origin master
git push copy master

Create a new remote named “all”, and add GitLab and GitHub URLs to it

git remote add all <GitLab URL>```
git remote set-url all --add --push <GitLab URL>
git remote set-url all --add --push <GitHub URL>
git push all main

Have comments use the comment box below.

Share: