Both Github and Bitbucket are good SCM hosting services. For some reason, you probably want to migrate your project from Github to Bitbucket or vice versa. It’s easy because you just need to change the remote repository info. Sometimes, you want to keep your code in both places which means you want to push it to both Github and Bitbucket.
Pushing to both Github and Bitbucket is easy.
Here is a project cloned from Github. If you check out the remote repository info, it has only Github repo info.
|
1 2 3 4 |
$ git remote -v show origin git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (fetch) origin git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (push) |
The YOUR_PROJECT/.git/config file, of course, has only Github info.
|
1 2 3 4 |
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git |
To add a remote git repository on Bitbucket, use git remote set-url.
|
1 2 |
$ git remote set-url origin --add ssh://git@bitbucket.org/YOUR_USERNAME/your_project.git |
Now, it has Bitbucket info too.
|
1 2 3 4 5 |
$ git remote -v show origin git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (fetch) origin git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (push) origin ssh://git@bitbucket.org/YOUR_USERNAME/your_project.git (push) |
YOUR_PROJECT/.git/config
|
1 2 3 4 5 |
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git url = ssh://git@bitbucket.org/YOUR_USERNAME/your_project.git |
Now, run git push -u origin master to set upstream.
|
1 2 |
$ git push -u origin master |
It’s enough for pushing to both Github and Bitbucket. So once it’s done, you can just do
|
1 2 |
$ git push |
Whenever you want to push to both.
e.g.)
Make some changes, Commit and Push
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# Add all changes $ git add -A # Commit changes $ git commit -m "added: .gitignore" [master 0e649a6] added: .gitignore 1 file changed, 1 insertion(+) create mode 100644 .gitignore # Push to Github and Bitbucket $ git push Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 286 bytes, done. Total 3 (delta 0), reused 0 (delta 0) To git@github.com:YOUR_USERNAME/YOUR_PROJECT.git a24a46c..0e649a6 master -> master Counting objects: 4, done. Delta compression using up to 4 threads. Compressing objects: 100% (2/2), done. Writing objects: 100% (3/3), 286 bytes, done. Total 3 (delta 0), reused 0 (delta 0) remote: bb/acl: YOUR_USERNAME is allowed. accepted payload. To ssh://git@bitbucket.org/YOUR_USERNAME/your_project.git a24a46c..0e649a6 master -> master |
If what you need is only to push to Github and Bitbucket then you don’t need to read this post any further. However, if you also want to pull data from both Github and Bitbucket, it requires a little more extra steps.
Before I show the steps, I need to point out another case that you might want to know. With what I’ve done here, it fetches the data only from Github when running the git pull command. If what you want is only changing it to make it pull from Bitbucket, it’s simple. You just need to swap the positions of origin urls in the YOUR_PROJECT/.git/config file.
So change from this
|
1 2 3 4 5 |
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git url = ssh://git@bitbucket.org/YOUR_USERNAME/your_project.git |
to
|
1 2 3 4 5 |
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = ssh://git@bitbucket.org/YOUR_USERNAME/your_project.git url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git |
Now it pulls from Bitbucket instead of Github.
To pull from both Github and Bitbucket, first add the remote repository information using git remote.
|
1 2 3 |
$ git remote add github git@github.com:YOUR_USERNAME/YOUR_PROJECT.git $ git remote add bitbucket git@bitbucket.org:YOUR_USERNAME/your_project.git |
You can add both Github and Bitbucket or just the one that is not pulled when running git pull. It’s all up to you.
Now, the YOUR_PROJECT/.git/config file contains the information of the new remote repositories just added.
|
1 2 3 4 5 6 7 8 9 |
$ git remote -v show bitbucket git@bitbucket.org:YOUR_USERNAME/your_project.git (fetch) bitbucket git@bitbucket.org:YOUR_USERNAME/your_project.git (push) github git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (fetch) github git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (push) origin git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (fetch) origin git@github.com:YOUR_USERNAME/YOUR_PROJECT.git (push) origin ssh://git@bitbucket.org/YOUR_USERNAME/your_project.git (push) |
YOUR_PROJECT/.git/config now has “github” and “bitbucket” as well as “origin”.
|
1 2 3 4 5 6 7 8 9 10 11 12 |
[remote "origin"] fetch = +refs/heads/*:refs/remotes/origin/* url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git url = ssh://git@bitbucket.org/YOUR_USERNAME/your_project.git [remote "github"] url = git@github.com:YOUR_USERNAME/YOUR_PROJECT.git fetch = +refs/heads/*:refs/remotes/github/* [remote "bitbucket"] url = git@bitbucket.org:YOUR_USERNAME/your_project.git fetch = +refs/heads/*:refs/remotes/bitbucket/* |
If you run git pull, it still pulls from only Github so you need to also run git pull bitbucket master.
* pull from Github
|
1 2 |
$ git pull |
or
|
1 2 |
$ git pull github master |
* pull from Bitbucket
|
1 2 |
$ git pull bitbucket master |
If you want to pull it from all the remote repositories, you can use a function in shell script.
Add the following function to .bash_aliases or .bashrc or .profile depending on your OS (sorry Windows users).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
gitpullall() { repos_to_ignore="origin github" should_ignore=-1 echo "running: git pull" git pull echo "" for remote in `git remote` do for ignore in $repos_to_ignore do [ "$remote" == "$ignore" ] && should_ignore=1 || : done if [ "$should_ignore" == "-1" ] then echo "running: git pull $remote master" git pull $remote master echo "" fi done } |
Then you can simply run gitpullall to pull from all the remote repositories specified in the YOUR_PROJECT/.git/config file.
To ignore repositories when pulling, add the remote repository names to the repos_to_ignore variable in the gitpullall function above.
|
1 2 3 4 5 6 7 8 9 10 |
$ gitpullall running: git pull Already up-to-date. running: git pull bitbucket master From bitbucket.org:YOUR_USERNAME/your_project * branch master -> FETCH_HEAD Already up-to-date. |


Recent Comments