Convert a Mercurial Repository to Git Using hg-fast-export
If you need to move your Mercurial repositories over to Git, then you should know about this tool. Read on to find out how to make your conversion (and life) easier.
Join the DZone community and get the full member experience.Join For Free
For the longest time, on my personal and side projects, I leveraged Mercurial as a source control system. It worked great, and I was always happy with it; however, I recently came to the decision that I wanted to start using Git for my source control. I've been using Git a lot at my primary employment and found it would be faster, easier, and overall better for my personal and side project source control. As a result, I decided to switch from Mercurial to Git, and have put together this little tutorial on how to convert a Mercurial repository to Git using hg-fast-export.
To convert my Mercurial repositories to Git, I decided to take advantage of a utility called hg-fast-export.
NOTE: This assumes you already have Git installed on your environment. If you do not have Git installed, please visit https://git-scm.com/ to download and install the latest release of Git to your environment.Step 1. Download hg-fast-export
To convert a Mercurial repository to Git, I recommend using a tool called hg-fast-export, which you'll need to use git to clone.
Step 2. Create a New Git Repository
git clone git://repo.or.cz/fast-export.git
The next step is to create and initialize a new empty Git repository.
Step 3. (Optional) Configure core.ignoreCase
mkdir new_git_repository cd new_git_repository git init
If you find hg-fast-export complaining about the option
core.ignoreCase being set to true in the Git repository, risking possible empty changesets for renamed files, you can set the option
core.ignoreCase in the Git repository to false to ensure that renamed files add to the changesets.
Step 4. Execute hg-fast-export
git config core.ignoreCase false
The next step is to run the hg-fast-export utility from the newly created and initialized Git repository folder, specifying the path to the Mercurial repository you wish to export and import it back into the Git repository.
Step 5. Checkout HEAD
/path/to/hg-fast-export.sh -r /path/to/hg_repo
The hg-fast-export utility does not automatically checkout the newly imported repository. You will need to follow up the import with a git checkout command to checkout the HEAD revision.
Step 6. Push Git Repository Server-Side
git checkout HEAD
To push the newly populated Git repository to a remote server, you'll need to add a remote origin location and execute a push to the remote origin/master branch.
Step 7. Copy Over Untracked Files and Update .gitignore
git remote add origin git@my-git-server:my-repository.git git push -u origin master
Once this has been completed, you'll also need to copy over any untracked files from the Mercurial repository to the Git repository, and modify the .gitignore file to specifically ignore the untracked files. You'll then want to commit the .gitignore file to your local repository and push updates out to the remote server.
Published at DZone with permission of Drew Harvey, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
How To Integrate Microsoft Team With Cypress Cloud
Step Into Serverless Computing
Tactics and Strategies on Software Development: How To Reach Successful Software [Video]
Top 10 Pillars of Zero Trust Networks