如何在同一个项目中同时使用 Bitbucket 和 GitHub?

我有一个存储库,我想推到 Bitbucket 和 GitHub。 对于我的存储库来说,由两者共同托管是至关重要的。

在 Git 中有办法做到这一点吗?

39455 次浏览

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 git@bitbucket.org: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://git@bitbucket.org/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://git@bitbucket.org/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.

I have a similar kind of situation with a twist. Like I have an existing BitBucket branch(s) where I was managing or updating my code form past few months.

Now my requirement is to upload my project in GitHub with the only single 'master' branch.

Step 1 -

This is my existing BitBucket project repo info.

$ git remote -v show


origin  https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git (fetch)
origin  https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git (push)

Step 2 -

add a remote github repo url -

$ git remote set-url origin --add https://github.com/<USERNAME>/<PROJECT_NAME>.git

Now, it has github info too ($ git remote -v show).

origin  https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git (fetch)
origin  https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git (push)
origin  https://github.com/<USERNAME>/<PROJECT_NAME>.git (push)

Step 3 -

Rename repository for better understanding -

$ git remote add bitbucket https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME>.git
$ git remote add github https://github.com/<USERNAME>/<PROJECT_NAME>.git

Now,info updated ($ git remote -v show).

bitbucket   https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME> (fetch)
bitbucket   https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME> (push)
github      https://github.com/<USERNAME>/<PROJECT_NAME>.git (fetch)
github      https://github.com/<USERNAME>/<PROJECT_NAME>.git (push)
origin      https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME> (fetch)
origin      https://<USERNAME>@bitbucket.org/<USERNAME>/<PROJECT_NAME> (push)
origin      https://github.com/<USERNAME>/<PROJECT_NAME>.git (push)

Step 4 -

Time to commit/push the whole project in GitHub.

$ git add --all && git commit -m "first commit"


$ git push -u origin master

As a result I got this error -

Everything up-to-date
Branch 'master' set up to track remote branch 'master' from 'origin'.
remote: Repository not found.
fatal: repository 'https://github.com/<USERNAME>/<PROJECT_NAME>.git/' not found

Step 5 (SSH key setup) -

After few hours of investigation I have found that this is SSH key issue.

So I generate SSH key for both BitBucket & GitHub and add those keys in my both respective account.

Step 6 (set SSH repo url) -

Change https:// urls to ssh for BitBucket & GitHub.

$ git remote set-url bitbucket git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git
$ git remote set-url github git@github.com:<USERNAME>/<PROJECT_NAME>.git

Remove origin to change origin repo url.

$ git remote rm origin

Add first origin (BitBucket) -

$ git remote add origin git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git

Add second origin (GitHub) -

$ git remote set-url origin --add git@github.com:<USERNAME>/<PROJECT_NAME>.git

All repo url changed to ssh.

$ git remote -v show


bitbucket    git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git (fetch)
bitbucket    git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git (push)
github       git@github.com:<USERNAME>/<PROJECT_NAME>.git (fetch)
github       git@github.com:<USERNAME>/<PROJECT_NAME>.git (push)
origin       git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git (fetch)
origin       git@bitbucket.org:<USERNAME>/<PROJECT_NAME>.git (push)
origin       git@github.com:<USERNAME>/<PROJECT_NAME>.git (push)

Step 7 -

Already I have add and commit the code, so just I have to push.

$ git push -u origin master

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Finally, the whole project pushed to GitHub in master branch.


Push code to both branch -

$ git push

Push code to only GitHub or BitBucket -

$ git push github master or $ git push bitbucket master

Change branch -

$ git checkout <BRANCH_NAME>

Live saving infos -

  1. use Bitbucket and GitHub at the same time for one project
  2. remove the remote origin

Important Note

Step 5 & 6 are useful If you are using http:// repo url, then change it to ssh for maintaining repo in a better way.

OR it will be better to use Step 5 & 6 if someone found https:// repo url after Step 1 / $ git remote -v show

Ignore step 5 & 6 if anyone already have ssh repo.