2022 guides keyboard image

What you'll need to follow this guide

  • a BitBucket repo you want to migrate from
  • a GitHub repo you want to migrate to
  • the GitHub CLI

Concept

Migrate one more repositories from BitBucket to GitHub using a simple bash script.


Benefits

For a time, BitBucket was a great place to find free code repository hosting. In more recent years however, GitHub has also introduced a free tier that provides more bells and whistles:

  • Built-in vulnerability scanning
  • GitHub Actions (we'll play with these another time)

Implementation Guide

The Source (BitBucket)

  • Let's say you have a BitBucket repo, here I'll call mine awesomesauce bitbucket repo screenshot

  • And this repository has multiple branches (featured here: development, qa, and master) with a commit history that you wish you preserve bitbucket repo history screenshot

The Destination (GitHub)

Instead of creating repos at the desination manually, we'll be using the GitHub CLI in our script to create repos at the destination so make sure you set this up and are logged in.

The Bash Script

Here we'll call it bitbucket_to_github.sh

#!/bin/bash
BB_ORG=<your BitBucket organization name>
GH_ORG=<your GitHub organization name>

# List out all your repos here
for i in repositoryA repositoryB repositoryC; do

  ## Install and configure GitHub CLI https://github.com/cli/cli#installation
  # Create private repos
  gh repo create $GH_ORG/$i --private

  ## Regular git CLI
  # Check out existing repo from bitbucket
  git clone --mirror [email protected]:$BB_ORG/$i.git && cd $i.git

  # Display the branches that should be moving over
  git branch -a

  ### Add a new remote origin:
  git remote add neworigin [email protected]:$GH_ORG/$i.git

  ### Push all local branches and tags to neworigin
  # Push all local branches
  git push --all neworigin
  # Push all tags:
  git push --tags neworigin

  ### Garbage Collection
  # Cleanup checkout
  cd .. && rm -rf $i.git

done

Run the Script

Hint: Don't forget to chmod +x bitbucket_to_github.sh to give the script permissions to run

./bitbucket_to_github.sh 
✓ Created repository crungruang/awesomesauce on GitHub
Cloning into bare repository 'awesomesauce.git'...
Receiving objects: 100% (9/9), done.
Resolving deltas: 100% (1/1), done.
  development
* master
  qa
Enumerating objects: 9, done.
Counting objects: 100% (9/9), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 1.22 KiB | 1.22 MiB/s, done.
Total 9 (delta 1), reused 9 (delta 1), pack-reused 0
remote: Resolving deltas: 100% (1/1), done.
To github.com:crungruang/awesomesauce.git
 * [new branch]      development -> development
 * [new branch]      master -> master
 * [new branch]      qa -> qa
Everything up-to-date

Verify on GitHub

  • Looking on GitHub we see a familiar repository! github repo screenshot

  • Commit history is also present! github commit history screenshot


Conclusion

With just a little elbow grease on the script to plug in your specific values, you should easily be able to migrate from BitBucket to GitHub, while preserving all branches and commit history. Enjoy some of the perks on GitHub!


Considerations

  • GitHub has a Large File size limit of 100MB and this migration is known to fail if a file this large was ever part of your repo's live branch or history. I'll provide some ideas on dealing with this in another post.