fbpx

How to Migrate a Git Repository from Github to Bitbucket

By Bobby Gill on July 1, 2021

At Blue Label, we’ve recently migrated from using GitHub as the hosting for our source code repos to Bitbucket. As part of this, we’ve had to go through all of our client projects and migrate all of the repos we previously had hosted on GitHub into our corporate Bitbucket account via the CLI (Yes, I know there is an import wizard that can do this, however this is for the OG’s out there that trust nuthin but their own Terminal window). Overall it’s a pretty straight forward process, however there are enough tiny little gotchas that I thought it would be worth documenting the steps in a blog post:

1.) First, create new a repo on Bitbucket and take note of the URL (or SSH endpoint) of the new repo.

2.) In a terminal window, clone the existing project from GitHub:

git clone <URL>

3.) Update your local repo with the metadata (branch names, tags, etc.) from the GitHub remote by running:

git fetch --all

4.) Prior to migrating the repo to Bitbucket, we need to make sure that locally on this machine we have checked out all remote branches and have them in our local repo. To do this is a simple shell script that will checkout each branch of the repo to your local machine.

for remote in `git branch -r | grep -v master `; do git checkout --track $remote ; done

5.) (Optional) If you are using Git LFS for large files, then you need to also fetch and pull down any LFS files hosted on your repo. You can do that through this command:

git lfs fetch origin --all

6.) Now you are ready to push the repo up to the new Bitbucket address. First, we need to change the remote reference in our local Git repo to the new Bitbucket endpoint:

git remote set-url origin <BitBucket repo address>

7.) After this, we do a git push to push all branches up to the remote on Bitbucket:

git push --all

8.) We’re not done. The git push doesn’t push all the tags you might have on your current repository. To ensure the tags get migrated as well execute the following command:

git push --tags

9.) (Optional) Finally, if you are using Git LFS, we need to push all the LFS files we downloaded in step 5 to the new repo. Do this by executing the following command:

git lfs push --all

Note if you receive a “exit status 255 connection closed by remote host” error when attempting to do the git lfs push, this seems to be an issue with using the SSH endpoint for connecting to the Bitbucket repo. I was able to overcome this by switching to the HTTP endpoint and re-pushing.

And that’s it! At this point the Bitbucket repo is now a clone of the GitHub repo and you can safely retire the GitHub repo.