Convert Mercurial project to Git [duplicate]
Asked Answered
W

8

299

I need to convert a mercurial project to a git project, but I would like to keep the commit history intact. My current solution was to just remove hg related files and then git init && add manually the files I needed, but that would not keep the history. Are there any solutions to this?

Waler answered 16/4, 2013 at 13:1 Comment(7)
A git-hg utility for checking out and tracking a mercurial repo. github.com/offbytwo/git-hgPiezochemistry
You might also try Kiln Harmony which keeps both a git and mercurial version of a repo and syncs everything, so it's usable from both.Domineca
Better approach github.com/buchuki/gitifyhgMedici
@Ian and others. Its not actually a duplicate question, the other question is about migrating a specific folder, this one is about the entire repo. Although answers to this questions have been posted to that question, This is a different question. It should be marked as a related question.Bullnecked
As simineer said, this is not a duplicate. However all the answers on that other question are ignoring the fact that the questioner only wants a single file, and are answering this question instead. By far the best answer is this one.Afrikaans
git-remote-hg: #883952Pulliam
For reference sake, Kiln Harmony has been discontinued.Kitchenmaid
B
299

You can try using fast-export:

cd ~
git clone https://github.com/frej/fast-export.git
git init git_repo
cd git_repo
~/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo
git checkout HEAD

Also have a look at this SO question.


If you're using Mercurial version below 4.6, adrihanu got your back:

As he stated in his comment: "In case you use Mercurial < 4.6 and you got "revsymbol not found" error. You need to update your Mercurial or downgrade fast-export by running git checkout tags/v180317 inside ~/fast-export directory.".

Besiege answered 16/4, 2013 at 13:4 Comment(20)
As an additional note, you can also pass in -A with an authors map file if you need to map Mercurial authors to Git authors.Kenspeckle
n.b. Install Mercurial before executing Step 5 if you haven't already. mercurial.selenic.comStalky
If you have errors like "repository has at least one unnamed head", you can specify --force option to deal with broken tree.Monongahela
I just did this for my psutil project but tags and branches has not been migrated.Maroney
best answer on the internet, don't waste your time with hggit bs.Miscall
doesn't work out of the box for windows. therefore may as well not work for 97% of people - might be worth mentioning that you can use cygwin or similar in that case (even then it has hidden dependencies)Soutane
I added '-f' to forcefully migrate the repo in my case. Was getting a few errors here and there.Byler
The host name does not seem to be working anymore I am getting a "Unable to connect to repo.or.cz'Draw
Does not work on windows: first need to install the python mercurial module (pip install mercurial), which fails if there is no compiler handy (with error: Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat))Urbas
@Soutane doesn't work out of the box for windows. therefore may as well not work for 97% of people - That number (97%) you're mentioning is from 2005.Hinda
And the repo doesn't seem to be there anymore: fatal: unable to connect to repo.or.cz: repo.or.cz[0: 195.113.20.142]: errno=Connection refusedCompany
What if I have a bunch of binary files in mercurial stored as "large files"? What will happen to them?Greengage
Can you please link to the Git repo of Fast Export (I think from github.com/frej/fast-export ?) instead of just linking to a blog post about it or referencing it in your command line instructions.Chessboard
error: pathspec 'HEAD' did not match any file(s) known to git.Profitsharing
HEAD as the checkout target might not work if one has only pulled one branch (and not default) in their old/hg repository. replace it with the branch name in that case, else checkout won't show any files.Disrobe
I am on Windows, this solution does not workProfitsharing
@Serge - if you're on Windows and you understand what's going on there you should be capable of adapt these steps on your local Windows box as long as there are other errors (maybe in the fast-export tool) on your Windows box. Please be more specific about what exactly does not work.Besiege
In case you use Mercurial < 4.6 and you got "revsymbol not found" error. You need to update your Mercurial or downgrade fast-export by running git checkout tags/v180317 inside ~/fast-export directory.Meatman
doesn't work on mac os.Shayna
For windows, I spent a lot of time trying to get it work. In the end I used Windows Subsystem for Linux (WSL). It does at least remove a whole category of issues.Limburg
A
104

Ok I finally worked this out. This is using TortoiseHg on Windows. If you're not using that you can do it on the command line.

  1. Install TortoiseHg
  2. Right click an empty space in explorer, and go to the TortoiseHg settings:

TortoiseHg Settings

  1. Enable hggit:

enter image description here

  1. Open a command line, enter an empty directory.

  2. git init --bare .git (If you don't use a bare repo you'll get an error like abort: git remote error: refs/heads/master failed to update

  3. cd to your Mercurial repository.

  4. hg bookmarks hg

  5. hg push c:/path/to/your/git/repo

  6. In the Git directory: git config --bool core.bare false (Don't ask me why. Something about "work trees". Git is seriously unfriendly. I swear writing the actual code is easier than using Git.)

Hopefully it will work and then you can push from that new git repo to a non-bare one.

Afrikaans answered 18/1, 2016 at 11:18 Comment(8)
The push kept failing for me on the push with error "dulwich.errors.RefFormatError: refs/heads/ref". It turns out that I had several bookmarks. After deleting each bookmark, it stopped failing.Pimpernel
Per the hg-git documentation, "TortoiseHg comes with hg-git" and just needs to be enabled (as shown above). However, it is not included with the command line version of mercurial (It can be installed if desired). If you have both TortoiseHg & HG installed, you need to be sure the hg.exe from TortoiseHg is used, and not the one form the command line installation. If you get the error No module named hggit, try fully qualifying the hg command: "C:\Program Files\TortoiseHg\hg" push c:/path/to/your/git/repHyperon
Note that this method only seems to import the currently checked-out branch (I'm looking for a solution that will import the whole repository).Capuche
Wouldn't hg bookmark -r default master be better than hg bookmarks hg?Michelle
If the push keeps failing like GaTechThomas says above, you may want to use: "hg gclear" to do the clearing.Cogent
For the files to appear in the repo, do: git checkout hgEquanimous
Make sure you set git config --bool core.bare false correctly. I had a typo, git config --bool core.bar false and of course, git didn't complain.Thurman
If anybody else is struggling to get hggit working, it's been re-included in the THG installer as of v6.1.2. So simply upgrading the latest THG worked for me. Otherwise +1 for saving my bacon with this postLanglauf
B
83

If you want to import your existing mercurial repository into a 'GitHub' repository, you can now simply use GitHub Importer available here [Login required]. No more messing around with fast-export etc. (although its a very good tool)

You will get all your commits, branches and tags intact. One more cool thing is that you can change the author's email-id as well. Check out below screenshots:

enter image description here

enter image description here

Byler answered 21/1, 2016 at 13:43 Comment(8)
Does it also migrate issues and wikis?Gloriole
@Gloriole No only commits, branches & tagsByler
You need a paid plan on Github to create private repositories. Not a problem if what you're converting is a public source tree anyway, of course.Catcall
@Åsmund one could also use the tool, clone the git, then delete the public repo, if one is only worried about public exposure of the code when it is not readyTopdress
@Byler thanks for this! was going to use fast export until I saw github would solve this!Topdress
Do you know how to upload and convert a local mercurial repository?Renee
I have this GitHub importer and I saw that my history was not exactly the same as it was in Mercurial. The commits where not in date-order and I believe this is the main issue. Does anyone have any idea what could cause this?Tarantella
Mine is frozen on: "Detecting your project’s version control system…" when importing from BitBucketCacilie
P
21

Some notes of my experience converting Mercurial to Git.

1. hg-fast-export

Using hg-fast-export failed and I needed --force as noted above. Next I got this error:

error: cannot lock ref 'refs/heads/stable': 'refs/heads/stable/sub-branch-name' exists; cannot create 'refs/heads/stable'

Upon completion of the hg-fast-export I ended up with an amputated repo. I think that this repo had a good few orphaned branches and that hg-fast-export needs a somewhat idealised repo. This all seemed a bit rough around the edges, so I moved on to Kiln Harmony (http://blog.fogcreek.com/announcing-kiln-harmony-the-future-of-dvcs/)

2. Kiln

Kiln Harmony does not appear to exist on a free tier account as suggested above. I could choose between Git-only and Mercurial-only repos and there is no option to switch. I raised a support ticket and will share the result if they reply.

3. hg-git

The Hg-Git mercurial plugin (http://hg-git.github.io/) did work for me. FYI on Mac OSX I installed hg-git via macports as follows:

  • sudo port install python27
  • sudo port select --set python python27
  • sudo port install py27-hggit
  • vi ~/.hgrc

.hgrc needs these lines:

[ui]
username = Name Surname <[email protected]>

[extensions]
hgext.bookmarks =
hggit = 

I then had success with:

hg push git+ssh://[email protected]:myaccount/myrepo.git

4. Caveat: Know your repo

All the above are blunt instruments and I only pushed ahead because it took enough time to get the team to use git properly.

Upon first pushing the project per (3) I ended up with all new changes missing. This is because this line of code must be viewed as a guide only:

$ hg bookmark -r default master # make a bookmark of master for default, so a ref gets created

The theory is that the default branch can be deemed to be master when pushing to git, and in my case I inherited a repo where they used 'stable' as the equivalent of master. Moreover, I also discovered that the tip of the repo was a hotfix not yet merged with the 'stable' branch.

Without properly understanding both Mercurial and the repo to be converted, you are probably better off not doing the conversion.

I did the following in order to get the repo ready for a second conversion attempt:

hg update -C stable
hg merge stable/hotfix-feature
hg ci -m "Merge with stable branch"
hg push git+ssh://[email protected]:myaccount/myrepo.git

After this I had a verifiably equivalent project in git, however all the orphaned branches I mentioned earlier are gone. I don't think that is too serious, but I may well live to regret this as an oversight. Therefore my final thought is to keep the original anyway.

Edit: If you just want the latest commit in git, this is simpler than the above merge:

hg book -r tip master
hg push git+ssh://[email protected]:myaccount/myrepo.git
Pennant answered 13/12, 2015 at 19:38 Comment(2)
Fog Creek responded to my support ticket confirming that Kiln Harmony is no longer available.Pennant
blog.fogcreek.com shows SSL certificate error. I don't trust people who can not handle SSL properly and still do it.Receptacle
M
14

I had a similar task to do, but it contained some aspects that were not sufficiently covered by the other answers here:

  • I wanted to convert all (in my case: two, or in general: more than one) branches of my repo.
  • I had non-ASCII and (being a Windows user) non-UTF8-encoded characters (for the curious: German umlaute) in my commit messages and file names.

I did not try fast-export and hg-fast-export, since they require that you have Python and some Mercurial Python modules on your machine, which I didn't have.

I did try hg-init with TortoiseHG, and this answer gave me a good start. But it looked like it only converts the current branch, not all at once (*). So I read the hg-init docs and this blog post and added

[git]  
branch_bookmark_suffix=_bookmark

to my mercurial.ini, and did

hg bookmarks -r default master  
hg bookmarks -r my_branch my_branch_bookmark  
hg gexport

(Repeat the 2nd line for every branch you want to convert, and repeat it again if you should happen to do another commit before executing the 3rd line). This creates a folder git within .hg, which turns out to be a bare Git repo with all the exported branches. I could clone this repo and had a working copy as desired.

Or almost...

Running

git status

on my working copy showed all files with non-ASCII characters in their names as untracked files. So I continued researching and followed this advice:

git rm -rf --cached \*  
git add --all
git commit 

And finally the repo was ready to be pushed up to Bitbucket :-)

I also tried the Github importer as mentioned in this answer. I used Bitbucket as the source system, and Github did quite a good job, i.e. it converted all branches automatically. However, it showed '?'-characters for all non-ASCII characters in my commit messages (Web-UI and locally) and filenames (Web-UI only), and while I could fix the filenames as described above, I had no idea what to do with the commit messages, and so I'd prefer the hg-init approach. Without the encoding issue the Github importer would have been a perfect and fast solution (as long as you have a paid Github account or can tolerate that your repo is public for as long as it takes to pull it from Github to your local machine).


(*) So it looked like before I discovered that I have to bookmark all the branches I want to export. If you do and push to a bare (!) repo, like the linked answer says, you get all the branches.

Melanoma answered 6/10, 2016 at 22:6 Comment(3)
to solve encoding problem with hg-fast-export (not sure for github importer) you can pass parameter 'e' like this: -e cp1251Reviere
to solve file names encoding problem with hg-fast-export you can use '--fe' option like --fe cp1251Overexpose
"Repeat the 2nd line for every branch you want to convert"?! Really?! I have a big old repository with MANY feature branches that all should be kept...Receptacle
A
10

From:

http://hivelogic.com/articles/converting-from-mercurial-to-git

Migrating

It’s a relatively simple process. First we download fast-export (the best way is via its Git repository, which I’ll clone right to the desktop), then we create a new git repository, perform the migration, and check out the HEAD. On the command line, it goes like this:

cd ~/Desktop
git clone git://repo.or.cz/fast-export.git
git init git_repo
cd git_repo
~/Desktop/fast-export/hg-fast-export.sh -r /path/to/old/mercurial_repo
git checkout HEAD

You should see a long listing of commits fly by as your project is migrated after running fast-export. If you see errors, they are likely related to an improperly specified Python path (see the note above and customize for your system).

That’s it, you’re done.

Allyl answered 16/4, 2013 at 13:6 Comment(2)
This worked for me, but I had to add a step. fast-export broke for all but the most recent versions of Mercurial (wasn't available through apt) so you either have to manually install a shiny new Mercurial >4.6 or just downgrade fast-export by adding a git checkout 19aa906 as a new step 3 above to get to the last commit that workedTrivalent
hivelogic.com doesn't exist anymore.Receptacle
C
6

Another option is to create a free Kiln account -- kiln round trips between git and hg with 100% metadata retention, so you can use it for a one time convert or use it to access a repository using whichever client you prefer.

Cellulose answered 16/4, 2013 at 13:34 Comment(4)
How do you use kiln for converting?Kaylyn
Kiln repositories are usable by both git and hg. So you can import from one client, and then export from the other client, as many times as you like.Geophagy
Kiln Harmony was very nice while it lasted, but it was shut down in September 2016. (And Kiln's free tier is being shut off next month.)Carlenecarleton
Kiln was sold recently and has had a lot of downtime since. Their status pages don't reflect most of this, but it's about once / month. Would only recommend if you absolutely need Mercurial and can't find another host.Patinous
X
2

This would be better as a comment, sorry I do not have commenting permissions.

@mar10 comment was the missing piece I needed to do this.

Note that '/path/to/old/mercurial_repo' must be a path on the file system (not a URL), so you have to clone the original repository before. – mar10 Dec 27 '13 at 16:30

This comment was in regards to the answer that solved this for me, https://mcmap.net/q/101864/-converting-mercurial-folder-to-a-git-repository which is the same answer as the one marked correct here, https://mcmap.net/q/99800/-convert-mercurial-project-to-git-duplicate

This moved our hg project to git with the commit history intact.

Xylotomous answered 16/6, 2016 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.