May 8, 2013

Migrating from bitbucket to github

Migrating repositories between bitbucket and github couldn’t be simpler thanks to the design nature of DVCS like git because the entire history is already located on your machine within your projects .git folder. All you’re really doing is changing a remote.

Open a terminal and navigate to your project directory.

TLDR;

For those who just want the commands with no explanation

git remote rename origin bitbucket
git remote add origin git@github.com:[username]/[repo_name].git
git push -u origin master
git remote rm bitbucket

Explanation of steps

Check which remote(s) you have now:

$ git remote -v show
origin	git@bitbucket.org:[username]/[repo_name].git (fetch)
origin	git@bitbucket.org:[username]/[repo_name].git (push)

Now rename your existing origin to bitbucket (or whatever you want to call it):

git remote rename origin bitbucket

Double check your change:

$ git remote -v show
bitbucket	git@bitbucket.org:[username]/[repo_name].git (fetch)
bitbucket	git@bitbucket.org:[username]/[repo_name].git (push)

Now add github as your origin and push the repository:

git remote add origin git@github.com:[username]/[repo_name].git
git push -u origin master

Check what remotes you have:

$ git remote -v show
bitbucket	git@bitbucket.org:[username]/[repo_name].git (fetch)
bitbucket	git@bitbucket.org:[username]/[repo_name].git (push)
origin	git@github.com:[username]/[repo_name].git (fetch)
origin	git@github.com:[username]/[repo_name].git (push)

Removed the old remote (bitbucket):

git remote rm bitbucket

Finally, check your remotes:

$ git remote -v show
origin	git@github.com:[username]/[repo_name].git (fetch)
origin	git@github.com:[username]/[repo_name].git (push)

Note that at the end of this, you still have your code on bitbucket, you’ve just pushed your repository to github and pointed your origin remote to there. To fully clean up you need to delete your repo from bitbucket.

© Michael Sharman 2017