How can I recover a removed file in Mercurial (if at all)?

52,041

Solution 1

First, use hg grep to find the deleted file you wish to recover. The output of this command will show you the last revision for which the file was present, and the path to the deleted file. Second, run hg revert -r <revision number> <path to deleted file> The deleted file will now be in your working copy, ready to be committed back into head.

Solution 2

Quote from comment:

I set up a repository, committed all, Removed and then committed again

If this is the case then you just need to update the working directory to the previous revision:

$ hg update -C -r-2

Note the negative revision number. If the files you deleted aren't in the previous revision, you can find them by using:

$ hg log -v

Solution 3

Well this worked for me.

hg revert -r revision pathToTheFile

Solution 4

For Mercurial 1.6 and above

If you know the name of the delete file you can find its revision easily with:

hg log -r "removes('NAME.c')"

This will give you the revision in witch a file called NAME.c (in the root) is deleted.

Then you can revert the file to the previous revision with (like other answers):

hg revert -r <revision number> <path to deleted file>

You can use a file name pattern instead to adapt to what you know, for example you can use **/NAME.c to search in all directories. You can read about it in File Name Patters. And use this link to know about the new revset specifications.

Solution 5

An addition to the accepted answer - this is faster if you want to undo all removals in a commit. I deleted a large folder with a few hundred files in it and did hg addremove, which was not at all my intent, so had to undo all of those deletes.

Using Find deleted files in Mercurial repository history, quickly? + xargs + tr, revert all revision -3 removals to the version from revision -4:

hg log -r -3 --template "{rev}: {file_dels}\n" | tr ' ' '\n' | xargs hg revert -r -4 

Note that this will fail if any of your files have spaces in the name; http://hgbook.red-bean.com/read/customizing-the-output-of-mercurial.html doesn't appear to have any templates where {file_dels} is split by \n at the moment.

Share:
52,041

Related videos on Youtube

PurplePilot
Author by

PurplePilot

golang, microservices, postgresql, python, docker, SRE

Updated on February 21, 2020

Comments

  • PurplePilot
    PurplePilot over 4 years

    Accidentally, by using a GUI as opposed to CLI, I removed every file in a Mercurial project.

    I recovered with Revert ok and lost some work, which as I have time machine I could easily get back. But is there a way of un-remove/undelete such files? Trawled through the manual and googled but cannot see anything. Any plugins?

    I am probably answering my own question here but the files were gone from the directory and were not in the trash to recover so I am assuming Remove is irrevocable?

    p.s. I know that hg forget or hg remove -Af will remove without deleting from the directory but my question has to do with the error I made as opposed to cool thinking the action through.

    • tback
      tback over 14 years
      you should be able to checkout an older revision and be fine (of course this only works in case you didn't rewrite history)
    • PurplePilot
      PurplePilot over 14 years
      the problem was that i had committed, made edits, R(emoved). So after that sequence yes, i had got the previous commit so went back to that and as i have backup in the form of time machine i could get the last changes from then add them in and then commit. However if i had not had a backuop the changes would have been lost.
    • Michael Pryor
      Michael Pryor over 12 years
      Related question which lets you search just filenames (fast): stackoverflow.com/questions/1013550/…
  • PurplePilot
    PurplePilot over 14 years
    No this doesn't work. I am using a Mac with Mercurial Distributed SCM (version 1.4.1+20091201). I set up a repository, committed all, Removed and then committed again but this only brings back a list of the files marked as R(emoved) but does not actually bring the files back. If i mark the files as R(emoved) and Rollback before committing nothing happens either.
  • ataylor
    ataylor over 14 years
    After you've done the hg rollback, your repo will be rolled back, but your current working directory will still have your changes. Just tell hg to do a clean update to the rolled back revision, now on the tip: hg up -C.
  • mrucci
    mrucci over 14 years
    hg rollback will not recover removed file because the deletion of the file is not part of the commit transaction.
  • ataylor
    ataylor over 14 years
    That's right. If you're looking to get at edits that were not committed before the remove, they are lost.
  • nc.
    nc. almost 13 years
    worked nicely for me, thanks. was looking for the answer to "how do I reinstate a file I purposely deleted?"
  • Emil
    Emil over 10 years
    from eclipse this can be done by right clicking the file Select Team-->Revert
  • mpen
    mpen over 9 years
    Another way to find the revision containing the file is hg log -I '**file.ext'
  • Lukas Knuth
    Lukas Knuth over 8 years
    Just an FYI: You want the revision-number of the commit that still has the file, NOT the one that removed it!
  • raddevus
    raddevus over 3 years
    if you just need the last version checked in you can do : $ hg revert <path to deleted file> No need to provide <revision number> if you don't want to.
  • 0xC0000022L
    0xC0000022L over 2 years
    This is one of those features that sets Mercurial apart!