• mohit vashistha

Convert Mercurial Repo to Git

It was just 8 month back when we moved from Klin to Bitbucket, we have to convert the mercurial repos into sub repos as BitBucket don't allows to have repo beyond 2GB size. We had split our repos into small repos and moved to BitBucket.


And recently Atlassian has announced to stop support for mercurial from Feb-2020. It time we convert our repos from mercurial to git.


Converting mercurial to Git


Prerequisite

1. Ubuntu or any Linux machine, I prefer Ubuntu.

2. SourceTree or (Tortoise Hg and Tortoise Git)


Step1: Install Git & Mercurial on Linux.

Install Git:

sudo apt-get install git

Install Mercurial:

sudo apt-get install mercurial


Step2: Clone Fast Export toll from Git Hub, to convert mercurial repo to git

Create a folder in home (mkdir) with name FastExport and run following command in the dir

git clone https://github.com/frej/fast-export.git


Step3: Clone your mercurial repo or copy it from your machine to linux machine e.g MyHgRepo

hg clone https://your/hg/repo/address


Step4: Export your Mercurial repository to a new Git one

1. Create a new folder for your git repo e.g: MyRepoGit

2. Change directory to your new folder cd MyRepoGit

3. Initialize the git repo git init

4. Export the mercurial repo to git repo

../fast-export/hg-fast-export.sh -r ../MyHgRepo/


Step5: Delete already merged branches

In mercurial we can close branches but there reference remains. So when we export the repo using above tool branches that are already closed shows up. We can delete

already merged branches except dev and master (you can add your branch name that

you want to save) with following command:

git branch --merged | grep -v '\*\|master\|develop' | xargs -n 1 git branch -d


Step6: Manually delete orphaned branches

Although above command deletes the already merged branches but leave the

branches that are closed without merging, it’s better to delete these branch manually,

as it will give you control what you are doing. You can use SourceTree to do this.


Step7: Create a new repo on you GitHub or BitBucket server. It will give you repo URL

https://your/git/repo/address.git


Step8: Add remote to your git repo

git remote add origin https://your/git/repo/address


Step9: Push all branches to remote

git push -u origin –all
9 views0 comments

Recent Posts

See All