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

GitGithubBitbucket

Git Problem Overview


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?

Git Solutions


Solution 1 - 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 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

Solution 2 - Git

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

Solution 3 - Git

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.

Solution 4 - Git

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 [email protected]:<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    [email protected]:<USERNAME>/<PROJECT_NAME>.git (fetch)
bitbucket    [email protected]:<USERNAME>/<PROJECT_NAME>.git (push)
github       [email protected]:<USERNAME>/<PROJECT_NAME>.git (fetch)
github       [email protected]:<USERNAME>/<PROJECT_NAME>.git (push)
origin       [email protected]:<USERNAME>/<PROJECT_NAME>.git (fetch)
origin       [email protected]:<USERNAME>/<PROJECT_NAME>.git (push)
origin       [email protected]:<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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionerogolView Question on Stackoverflow
Solution 1 - GitDenys Kniazhev-Support UkraineView Answer on Stackoverflow
Solution 2 - GitKevin LeeView Answer on Stackoverflow
Solution 3 - GitAhmad AwaisView Answer on Stackoverflow
Solution 4 - GitPinakiView Answer on Stackoverflow