Migrating a private repository from Bitbucket to GitHub with Git

January 08, 2019

As GitHub private repositories have just become free, I'm jumping on the bandwagon and shipping over a few of the repos I have on Bitbucket to reduce the number of providers I store code with.

Preserved commits

The end result - a private GitHub repository with all the metadata from the old Bitbucket repository - note we have maintained the last commit from 10 months ago

The option below uses the shell to migrate the repo, but you can also use the GitHub importer if you prefer an automated solution (you'll just have to wait while it does it).

Four simple steps, using "add upstream"

1 Create a target repository on GitHub, either through the website, desktop app or shell

Use the tool of your choice to create a target repository on GitHub. Ensure you don't initialize it with a README or you'll need to resolve this later.

Create your new private repository

Make sure you don't initialize the repo with a README

2 Clone the Bitbucket repository onto your PC

Clone the repo onto your local system. I'm on Windows today, so that's:

D:\git>git clone https://[email protected]/withdave/wedding-website.git

3 Add the target GitHub repository as the upstream remote for the local copy you've just cloned from Bitbucket

D:\git\wedding-website>git remote add upstream https://github.com/withdave/wedding-website.git

4 Push the local copy (and if you have them, tags) to the target repository, and remove the Bitbucket copy

D:\git\wedding-website>git push upstream master

Optionally, if you have tags to push:

D:\git\wedding-website>git push --tags upstream 

Check the status, and if successful remove the Bitbucket repository to tidy up!

Alteratively (1/2): Using the GitHub Importer

GitHub offers an importer for internet accessible repositories, via the plus at the top right (once logged in).

GitHub Import

Click the + in the top right, next to the user menu

Simply specify the source URL and target repository name and it'll prompt you for the relevant information.

GitHub Import Page

You'll receive a notification once the import is complete

Alternatively (2/2): Use "mirror"

The GitHub help suggests using --mirror with the following commands as detailed on their help site.

$ git clone --bare https://bitbucket.com/withdave/wedding-website.git

$ cd wedding-website.git git push --mirror https://github.com/withdave/wedding-website.git 

$ cd .. 
$ rm -rf wedding-website.git 

This also maintains all branches and tags, but doesn't update the upstream repository - it mirrors the repository, and then removes this from the local machine once complete (so you'd need to clone it again from the target repository to work with it).

References


Profile picture

From Dave, who writes to learn things. Thoughts and views are his own.

© 2024, withdave.