(Automatically) Move my git repositories from Bitbucket to Github

If you're not interested into the backstory, but only want to know about the script, click here

Why, you might wonder?

Until today I was hosting my public repositories on Github and my private repositories on BitBucket. You might easily guess the reason: until few months ago to host private repositories on Github a subscription was required, and when I started having this requirement i could "not afford" the github pricing.
Now times have changed and not only I can afford Github Pro, but also this platform started offering free private repositories (thanks Microsoft), so I decided that I was going to simply merge all my codebases in one place, for pracicality.

The choice!

Well, I had to choose if move everything on Bitbucket or Github, this choice was actually rather easy.
While BB seems to be actually more feature rich then GH for my usage most features are not very useful and to be honest i find BB user interface horribly slow and not very pleasant, even tho I must admit they've done quite some progress in the past years.

So bottom line, without to much thinking Github was the choice.

How?

Well, given that I had around 80 private repositories to move, manual work was definitely not an option, therefore I went for my scripting language of choice: python!

The script itself is rather easy, it's broken down in a few simple steps:

  • Get all repos in BitBucket (and loop thought them)
  • Create a new repo on GitHub with the same slug name (ensures there isn't already one with the same name)
  • Does a bare clone of the BB repo and mirros it to GH

Nothing crazy, just a simple 150 lines script, you can view the source of it embedded at the bottom of this article or here on a gist on GH.

Comments?

If you have any comments please reach me via Twitter. 

How to run the script!

The only requirement is to have pipenv, installing it outside the scope of this article, but if are on macOS it could be simply running $ brew install pipenv, for more info consult the official documentation.

To run the script you should update the following variables, which can be found on top of the script:

  • APP_KEYS: A map containing the keys to authenticate on BB and GH.
  • BB_USER: Your username on BitBucket
  • GH_USER: Your username on GitHub
  • BB_EMAIL: The email which will be used to identify your client against the BitBucket API.

Once you have done this the job is simple, initialise the pipenv via $ pipenv install and once all dependencies have been installed just run the script with $ pipenv shell followed by $ python ./repository-migrate.py
If all went fine in a few minutes (speed depends on your connection and amount of repositores) all your repositores on BB will be mirrored on GH while preserving the "private" status!

The script

Don't forget that you'll also need the Pipfile, full gist source here!