Git, use of remotes

Posted on

I wrote not so long ago an article about how to create mirrors to Github. At that time, I thought I would use Github only to have a better exposition of what I was doing. Unfortunately let’s face it, living without Github if you are writing 3 lines of code on your side is quite useless. Just imagine, someone wants to send you a pull request and you can’t accept it because he should do that by registrating on your repository system or sending you patches through emails (that is still the best way though unless you’re using a crappy email client).

Anyway, there is a better way (I think) to still have the possibility to work on your own repository system and accepting commits or pull requests from other systems using remotes. Nothing fancy and a thread on StackOverflow has been very helpful for that. In order to do that, nothing difficult. First clone your repository and look at the remotes using git remote -v. You should have an output like:

origin  git://original/repo.git (fetch)
origin  git://original/repo.git (push)

From there, if you want to push to two or more repositories using a single command, you will need to create a new remote called all (or any name that suits you the best).

! You can even edit the origin but the name is less descriptive for this purpose.

So now, let’s add a new remote called all that we’ll reference later when pushing to multiple repositories:

$ git remote add all git://original/repo.git
$ git remote -v
all git://original/repo.git (fetch)               <-- ADDED
all git://original/repo.git (push)                <-- ADDED
origin  git://original/repo.git (fetch)
origin  git://original/repo.git (push)

Then let’s add a push url to the all remote, pointing to another repository:

$ git remote set-url --add --push all git://another/repo.git
$ git remote -v
all git://original/repo.git (fetch)
all git://another/repo.git (push)                 <-- CHANGED
origin  git://original/repo.git (fetch)
origin  git://original/repo.git (push)

Here git remote -v shows the new push url for push, so if you do git push all master, it will push the master branch to git://another/repo.git only. This shows how push url overrides the default url (remote.all.url).

Now let’s add another push url pointing to the original repository:

$ git remote set-url --add --push all git://original/repo.git
$ git remote -v
all git://original/repo.git (fetch)
all git://another/repo.git (push)
all git://original/repo.git (push)                <-- ADDED
origin  git://original/repo.git (fetch)
origin  git://original/repo.git (push)

You see both push urls we added are kept. Now a single git push all master will push the master branch to both git://another/repo.git and git://original/repo.git.