Migrating repository from Bit-bucket to GitHub

data codes through eyeglasses
Reading Time: 3 minutes

This blog provide you with knowledge to automate migrate a repository from Bit-bucket to GitHub. These are the following steps you need to take care of.

1. Generate ssh key

First, you’re going to need to generate an SSH key. Depending on the type of work you do, you may want to be careful with this. That is, if you have an existing id_rsa file, don’t overwrite it. Instead, we’ll create a second one.

To create an ssh key, type following in a terminal:

$ ssh-keygen

Note: Do the above for both GitHub and Bit-bucket and provide different names when prompted to do so. In my case, id_rsa_git for GitHub and id_rsa_bit for bit bucket.

2. Add the Key to Your Local Agent

Next, you’re going to need to add the key to your SSH agent. All of this is necessary so we can communicate with the proper GitHub account when it comes time to start migrating from Bitbucket to GitHub.

To add the key to your local agent, type following in terminal:

$ eval "$(ssh-agent -s)"

This will start the ssh-agent program as a background process. After that, enter the next command:

For eg:

$ ssh-add ~/.ssh/<private_key_file>

For GitHub:

$ ssh-add ~/.ssh/id_rsa_git

For bit-bucket:

$ ssh-add ~/.ssh/id_rsa_bit

3. Add the SSH key to GitHub

Next, log in to the GitHub account to which you’ll be migrating the repositories. In the account, click on your profile icon then click on the Settings menu item. Know now on Adding ssh key in GitHub.

 

From there, choose the option for SSH and GPG Keys. After that, copy the contents of the newly created key to your clipboard. You can do this in the terminal with the following command:

$ pbcopy < ~/.ssh/id_rsa_git.pub

Or

$ cat ~/.ssh/id_rsa_git.pub

and copy its content.

Next, click on the New SSH Key button and, on the following page, give the key whatever name you’d like and paste the contents that were just copied to your keyboard.

4. Add the SSH key to Bit-bucket

Next, login to the Bit-bucket account from which you’ll be migrating the repositories. In the account, click on your profile icon then click on the Personal Settings then SSH Keys. Know now on Adding ssh key in Bit-bucket.

You can do this in the terminal with the following command:

$ pbcopy < ~/.ssh/id_rsa_bit.pub

Or

$ cat ~/.ssh/id_rsa_bit.pub

and copy its content.

Next, click on the Add Key button and, on the following page, give the key whatever name you’d like and paste the contents that were just copied to your keyboard.

5. Create a GitHub PAT

Now, go to Settings, Developer settings, then click on Personal access tokens. Create a Personal access token (PAT) with admin permission to repository and organisation.

After you created a PAT, you will get a token. Make sure to save that token as it is visible only once, it is required for user authentication.

6. Run migration script

Now, copy the following code in a file named “migrationBBtoGH.sh”.

#!/bin/bash

echo "... Processing $REPO ..."
echo

# Setting a mirror of source repository
git clone --mirror git@bitbucket.org:$BIT_USERNAME/$REPO.git
cd $REPO.git
echo
echo "... $REPO cloned, now creating on github ..."  
echo

# Creating a repository on github
curl -u $GIT_USERNAME:$GIT_PASSWORD https://api.github.com/user/repos \
     -d "{\"name\": \"$REPO\", \"private\": true}"
echo
echo "... Mirroring $REPO to github ..."
echo

# Pushing mirror to github repository
git push --mirror git@github.com:$GIT_USERNAME/$REPO.git
cd ..

Now, export these environment variables and run the script

$ export GIT_USERNAME=<your GitHub username>
$ export GIT_PASSWORD=<your GitHub PAT token> 
$ export BIT_USERNAME=<your Bit-bucket username>
$ export REPO=<your Bit-bucket migrating repository name>
$ sudo chmod +x migrationBBtoGH.sh
$ ./migrationBBtoGH.sh

This should completely migrate your Bit-bucket repository to GitHub with all tags and commit history. Know more about GitHub Action and Self-hosted runner in GitHub.

Leave a Reply