How to use Bitbucket and GitHub at the same time for one project?

I have one repository which I want to push into Bitbucket and GitHub. It is vital for my repository to be hosted by both.

Is there a way to do this in Git?


You can use multiple remote repositories with git. But you'll have to push separately into 2 of your remotes I believe.

For example, if your project currently points to github, you can rename your current remote repository to github:

$ git remote rename origin github

You can then add another remote repository, say bitbucket:

$ git remote add bitbucket [email protected]:your_user/your_repo.git

Now in order to push changes to corresponding branch on github or bitbucket you can do this:

$ git push github HEAD
$ git push bitbucket HEAD

Same rule applies to pulling: you need to specify which remote you want to pull from:

$ git pull github your_branch
$ git pull bitbucket your_branch

Yes, you can do that. You don't have to push twice but just once to push to both remote repositories. I had the same issue before so wrote how to do it here. Git: Push to / Pull from Both Github and Bitbucket


A few EASY solutions.

Multiple remotes pushed (and fetched) independently

This is the easiest to get your head around, but the most effort to maintain.

We start out by adding our new remote:

$ cd myproject 
$ git remote add bitbucket ssh://[email protected]/user/myproject.git 
$ git push bitbucket master

Straight forward no? Except of course every time we commit any changes, we need to push to both our original “origin” and our new remote “bitbucket”:

$ git push origin master
$ git push bitbucket master

Not a massive overhead, but I’m sure it will grate over time. Or you can create an `alias gpob="git push origin master && git push bitbucket master".

Single remote with multiple URLs pushed (and fetched) consecutively

With this method, we are going to add an additional URL to our existing remote “origin”:

$ cd myproject
$ git remote set-url --add origin ssh://[email protected]/user/myproject.git
$ git push origin master
Everything up-to-date
Everything up-to-date

Much less effort!

Of course silver lining has a cloud, and in this case, it is that while we can push to multiple URLs simultaneously, we can only fetch from the original “origin” (you can change this, but that is out of scope for this post).

Finally, to see which remote will be fetched from:

$ git remote -v show

I blogged about it as well.