Featured image of post Migration from BitBucket to Forgejo

Migration from BitBucket to Forgejo

Let’s discuss about how we can migrate from BitBucket to Forgejo (self hosted git server).
It works with any git server.

Back Story

I have more than 20 repositories in BitBucket back when there is no support for private repositories in Github. Since BitBucket provided private repos I used it extensively for all my personal projects.

Later GitHub provided private repos and I’m using that from then on.

Fast forward from 10 years to now, I want to consolidate all my repositories into one place and I have a personal Git server using Forgejo and I would like to move all repos from BitBucket to Forgejo.

Quirks

I have checked in Forgejo migration UI for BitBucket but it is not available.

Migrations available in Forgejo

I checked if any effort is made into making a migration for BitBucket and there is an issue for that but it is not being actively worked upon.

Getting data from BitBucket

At first try to setup SSH access for BitBucket

Generating SSH Key using ssh-keygen

Replace <username> with your actual username from BitBucket or use any other file name of your choice
One can use a different algorithm and use password for SSH key for more security.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
ssh-keygen 
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/bilbo/.ssh/id_ed25519): <username>@bitbucket.org
Enter passphrase for "<username>@bitbucket.org" (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in <username>@bitbucket.org
Your public key has been saved in <username>@bitbucket.org.pub
The key fingerprint is:
SHA256:uzFFK04O2uKc3oynJqSDs6rcEXXXXXvDezEyDBcoH8Mc xxxx@xxxxx
The key's randomart image is:
+--[ED25519 256]--+
|.                |
|.. .             |
| .o E     .      |
|...o     . .     |
|.. .. . S o      |
|o.oo o = +       |
|o*o = . *        |
|B.OO.B.  +       |
|*Xo*%oo .        |
+----[SHA256]-----+

Now we will end up with private and public keys which is called key pair.

Copy the public key

1
cat <username>@bitbucket.org.pub

Note:

  1. Copy the entire public key even dashes
  2. Replace <username> with your username or your public key file name

Setup SSH key in BitBucket

Goto Settings (Top right in UI)-> Personal BitBucket Settings -> SSH keys

Personal BitBucket settings

BitBucket SSH keys

Click on Add Key

A modal popup comes up

BitBucket Add SSH key

Give the SSH key a name and paste the public key from previous step in to public key field
You might want to give it an expiry to limit dangling SSH keys to have access

Once SSH key is setup properly try to see if that is working

1
2
3
4
5
# Replace <username> with your user name or your private key file name
ssh -i <username>@bitbucket.org -T git@bitbucket.org
authenticated via ssh key.

You can use git to connect to Bitbucket. Shell access is disabled

Successfully logged into BitBucket

Setup SSH Hosts for easy access

Copy below information into your ssh config (~/.ssh/config)

1
2
3
4
Host bitbucket.org
    HostName bitbucket.org
    User git
    IdentityFile <path to your bitbucket private key>

Note: Replace <path to your bitbucket private key> with your original path

Then we can test it using

1
2
3
4
ssh -T git@bitbucket.org
authenticated via ssh key.

You can use git to connect to Bitbucket. Shell access is disabled

Setup repository from BitBucket

Change directory to any folder you want to clone into

1
2
# Replace <username> with your username and <repo> with your repo
git clone git@bitbucket.org:<username>/<repo>.git

Change the remote url as we would push this repository into different git server

1
2
3
4
# Replace <new git server url>, <username>, <repo> with your information
# git remote set-url origin ssh://<new git server url>/<username>/<repo>.git
# Example
git remote set-url origin ssh://git@minastirith.xxxx/vbsampath/tutorials.git

Note: Setup remote url while being in git repository

Now check if the url is set properly

1
2
3
git remote -v
origin  ssh://git@minastirith.xxxx/vbsampath/tutorials.git (fetch)
origin  ssh://git@minastirith.xxxx/vbsampath/tutorials.git (push)

Uploading to new Git server

1
git push origin --all

Above command works for single branch but I found out for repos with multiple branches it is not working.

Trials

1
git push origin 'refs/tags/*' 'refs/heads/*'

1
git push origin '*:*'

1
2
3
4
git clone --mirror  OLD_GIT_URL
cd  NEW_CREATED_FOLDER
git remote add NEW-REMOTE NEW_GIT_URL
git push  NEW-REMOTE --mirror 

All of the above didn’t work with multiple branches

Solution for multiple branches upload

1
2
3
for remote in `git branch -r `; do git checkout --track remotes/$remote ; done # This step is tracking all branches
git push -u origin --all # All work is being handled here
git push -u origin --tags

Now check on new git server if all branches are migrated.

BitBucket to Forgejo migration

Conclusion

There is an option in Forgejo migration UI for importing git repository. If you have couple of repositories then we can use UI to migrate.

I choose this approach because I have many repositories and want to automate it using a script and that can be used for other Git servers as well in future

Note: Above method might not migrate issues but I don’t have any issues in my repositories so I’m good for now.

Below are the websites I used for my migration effort