How To Convert A Mercurial Repository To Git On Windows

Có thể bạn quan tâm

There are various guides on the internet to converting a Mercurial repository into a git one, but I found that they tended to assume you had certain things installed that might not be there on a Windows PC. So here’s how I did it, with TortoiseHg installed for Mercurial, and using the version of git that comes with GitHub for Windows. (both hg and git need to be in your path to run the commands shown here).

Step 1 – Clone hg-git

hg-git is a Mercurial extension. This seems to be the official repository. Clone it locally:

hg clone https://bitbucket.org/durin42/hg-git

Step 2 - Add hg-git as an extension

You now need to add hg-git as a mercurial extension. This can either be done by editing the mercurial.ini file that TortoiseHg puts in your user folder, or just enable it for this one repository, by editing (or creating) the hgrc file in the .hg folder and adding the following configuration

[extensions] hggit = c:/users/mark/code/temp/hg-git/hggit

Step 3 – Create a bare git repo to convert into

You now need a git repository to convert into. If you already have one created on GitHub or BitBucket, then this step is unnecessary. But if you want to push to a local git repository, then it needs to be a “bare” repo, or you’ll get this error: abort: git remote error: refs/heads/master failed to update. Here’s how you make a bare git repository:

git init --bare git_repo

Step 4 – Push to Git from Mercurial

Navigate to the mercurial repository you wish to convert. The hg-git documentation says you need to run the following one-time configuration:

hg bookmarks hg

And having done that, you can now push to your git repository, with the following simple command:

hg push path\to\git_repo

You should see that all your commits have been pushed to git, and you can navigate into the bare repository folder and do a git log to make sure it worked.

Step 5 – Get a non-bare git repository

If you followed step 3 and converted into a bare repository, you might want to convert to a regular git repository. Do that by cloning it again:

git clone git_bare_repo git_regular_repo Tweet

Comments

Comment by Patrick June 29. 2019 12:14

The git repository needs a hint which branch to use, I found help at https://stackoverflow.com/questions/10710250/converting-mercurial-folder-to-a-git-repository/31827990#31827990, that uses not a bare repository and at the end does a checkout to get the hg tag to the master branch.

Patrick Comment by Enrico Siboni January 15. 2020 22:34

This is the first result for "mercurial to git migation" query... but it doesn't work as is...I find this thing frustrating for developers needing a fast way to do what they want...I found the way to do that and now i'm sharing my findings...Replace Step 4 first commad hg bookmarks hg with hg bookmark -r default masterThis solved for me the annoying "conversion fail" problem

Enrico Siboni Comment by Mark Heath January 15. 2020 22:37

Thanks for sharing your solution. This post is 7 years old now, so not surprising it doesn't work anymore

Mark Heath Comment by Remco February 10. 2020 20:42

It does, really, just did it.

Remco Comment by Remco February 10. 2020 20:44

Thanks for the reference. This worked like a charm, for me at least.Did have some issues, but updating mercurial solved most of those.Also, knowing that master isn't the latest branch since it's renamed to hg does help.Glad I could finally convert without too much of an issue. Although i prefer hg over git when it's about comprehendability, git has become the default standard. Thanks again for this old, but useful post.

Remco

About Mark Heath

Microsoft MVP

I'm a Microsoft MVP and software developer based in Southampton, England, currently working as a Software Architect for NICE Systems. I create courses for Pluralsight and am the author of several open source libraries. I currently specialize in architecting Azure based systems and audio programming. You can find me on:

Search this site

Keep Updated

Subscribe to our mailing list

My Pluralsight Courses

  • Microservices: Architectural Strategies and Techniques
  • Azure Functions Deep Dive
  • Microservices Fundamentals
  • Refactor and Optimize Code with GitHub Copilot
  • LINQ Best Practices
  • Microsoft Azure Developer: Deploying and Managing Containers
  • Azure Functions Fundamentals
  • Azure Durable Functions Fundamentals
  • Microservices Architecture: Executive Briefing
  • Building Microservices
  • Dapr 1 Fundamentals
  • Create Serverless Functions
  • More Effective LINQ
  • Versioning and Evolving Microservices in ASP.NET Core
  • Microsoft Azure Developer: Implement Azure Functions (AZ-204)
  • Azure Container Instances: Getting Started
  • Building Serverless Applications in Azure
  • Azure CLI: Getting Started
  • Understanding and Eliminating Technical Debt
  • Windows Forms Best Practices
  • Digital Audio Fundamentals
  • UWP Audio Fundamentals
  • Audio Programming with NAudio
  • Understanding Distributed Version Control Systems
  • Creating Modern WPF Applications with MahApps.Metro
  • Creating and Selling a Digital Product
  • ClickOnce Deployment Fundamentals

My Open Source Software

  • NAudio
  • NLayer
  • NAudio.SharpMediaFoundation
  • WaveForm Renderer
  • WPF WaveForm
  • TypeScript Tetris
  • Skype Voice Changer
  • MIDI File Mapper
  • MIDI File Splitter
  • SilverNibbles
  • Asterisk
  • WPF Shapes
  • .NET Voice Recorder

Archives

Tags

  • Azure (160)
  • NAudio (103)
  • Advent of Code (82)
  • LINQ (79)
  • C# (71)
  • F# (70)
  • Azure Functions (69)
  • Azure CLI (47)
  • WPF (39)
  • audio (38)
  • XAML (38)
  • JavaScript (36)
  • Silverlight (34)
  • Pluralsight (30)
  • Durable Functions (26)
  • Docker (26)
  • MoreLINQ (26)
  • Azure Blob Storage (21)
  • Azure Service Bus (17)
  • PowerShell (16)

Từ khóa » Hg 2 Git