Most of the providers do provide an way to import a repo using GUI by entering the URL of the source and credentials (if the repo requires auth). But sometimes it might be convinient to do it using command line as this approach is going to be the same across providers.

How to import the repository using command line

In command line, it is just a matter of executing five lines of commands and you can move repos from one provider to another be it Bitbucket to Github or Github to Gitlab or Gitlab to Bitbucket or any other combination. Here are the steps, but read the notes given below before executing them:

$ git clone --bare https://oldhost/../reponame.git
$ cd reponame.git
$ git push --mirror https://newhost/../reponame.git
$ cd ..
$ rm -rf reponame.git

Here are the explaination for each of the steps:

  • git clone –bare https://oldhost/../reponame.git

    Clone the existing repo using the clone command. Do make a note of the use of the --bare option, this makes a clone without a working directory (i.e. no working or checked out copy of your source files).

  • cd reponame.git

    Move into the cloned folder. Note that bare repositories are given a .git extension generally.

  • git push –mirror https://newhost/../reponame.git`

    This pushes the mirror to the repo (you need to create it prior) on the new providers side.

  • cd ..

    Move out of the repo directory.

  • rm -rf reponame.git

    Remove the directory as there is no need for it any longer. If you want to continue working, do a fresh clone without the bare parameter.

Use case

I recently moved my primary blog which has been on WordPress for past 15 odd to a static build site. To be able to fully automate it I initially took the path of local development >> Bitbucket >> Pipeline >> AWS S3 & Cloudfront. Since I was already using the same stack Bitbucket at work, this was a straight forward task. But I was thinking of simplying it further and saw CloudFlare Pages which provided a quick and easy way to deploy such static sites. So I went about setting it up, that’s where I ran into the need to migrate from Bitbucket to Github as they supported only Github and Gitlab.