Saturday, November 5, 2011

[General Dev]–How to push to two Git remote locations at once

Now that BitBucket supports Git, I’ve been utilizing both GitHub and BitBucket for my project hosting, and have been looking into migrating to BitBucket for my private repositories (as they are free). This is part of the beauty of using a distributed version control system, you can have your code in multiple locations, rather than one central Subversion server.
In Git, I could add two remotes and each time I want to push/pull code I would type something like:
git push github master
git push bitbucket master
However, I want to make things as easy as possible for myself, and I’m simply using these two repositories as a mirror of each other, so I’ll be telling git to push to multiple URL’s with the same remote.
To do this, I need to edit the .git/config file for my project. You can navigate here via the command line, or in Windows you can turn on hidden file display and open the .git folder in Windows Explorer
image
image
In this file, I’ve already added one of my remotes using the git remote add command, as you can see here:
image
To make it push to BitBucket at the same time as GitHub, you simply duplicate this URL line and add the BitBucket URL to it
image
Then if you do a git push origin master (where master is name of your branch), you will see it will push to these URLs in succession:
image
Unfortunately if you have a password protected ssh key it will still ask twice for your password.
Now each time you push to your origin remote, you’ll be taking full advantage of gits distributed nature, and will have two remote copies of your code.

5 comments:

Adam Piper said...

$ eval $(ssh-agent)
$ ssh-add

Now you only add your password once. Depending on your distro you may already have ssh-agent loaded, so first try just a straight:

$ ssh-add

Anonymous said...

Use an ssh agent to remember your key.

Shengbin Meng said...

Great post!
"(where origin is name of your branch)": this may be wrong. master is name of branch, while origin is name of the remote.

Shengbin Meng said...

Great post!
"(where origin is name of your branch)" may be wrong. origin is the name of a remote, master is name of your branch.

Rod Howarth said...

Thanks Shengbin, corrected that now