21

At some stage in the past I had a "foo.txt" which was under Mercurial source control. However it has now been deleted.

How can I recover the file when I don't know the last Mercurial revision in which the file was deleted?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Stormcloud
  • 2,065
  • 2
  • 21
  • 41
  • See also [Find deleted files in Mercurial repository history, quickly?](https://stackoverflow.com/questions/1013550/find-deleted-files-in-mercurial-repository-history-quickly) for more about locating it. – Joshua Goldberg Oct 06 '16 at 19:46

2 Answers2

22

If you know the exact path for the file, you can do something like :

hg log -l 1 path/to/foo.txt

This will show you the last changeset where foo.txt was modified, so you will be able to restore the file from this revision.

Once you have the right revision, you can simply do :

hg revert -r <my revision> path/to/foo.txt
hg commit -m "add the foo.txt file again"
krtek
  • 26,334
  • 5
  • 56
  • 84
  • 3
    I couldn't get your log command to work. It would not show which revision that the delete occurred. I had to do `hg log -l 1 --removed path/to/foo.txt`. – Brian Neal Sep 01 '13 at 19:06
9

Using revsets:

hg log -r "removes('path_to_file')"

Where path_to_file can be anything documented in hg help patterns, including an exact path, a glob or a regular expression.

StayOnTarget
  • 11,743
  • 10
  • 52
  • 81
anton.burger
  • 5,637
  • 32
  • 48