106

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.

Rod
  • 52,748
  • 3
  • 38
  • 55
PurplePilot
  • 6,652
  • 7
  • 36
  • 42
  • 1
    you should be able to checkout an older revision and be fine (of course this only works in case you didn't rewrite history) – tback Feb 01 '10 at 09:03
  • 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. – PurplePilot Feb 02 '10 at 09:39
  • 1
    Related question which lets you search just filenames (fast): http://stackoverflow.com/questions/1013550/find-deleted-files-in-mercurial-repository-history-quickly – Michael Pryor Jan 19 '12 at 22:18

9 Answers9

157

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.

BungleFeet
  • 1,795
  • 1
  • 12
  • 7
  • 5
    worked nicely for me, thanks. was looking for the answer to "how do I reinstate a file I purposely deleted?" – nc. Jul 04 '11 at 11:59
  • from eclipse this can be done by right clicking the file Select Team-->Revert – Emil Oct 10 '13 at 06:43
  • 3
    Another way to find the revision containing the file is `hg log -I '**file.ext'` – mpen Feb 20 '15 at 23:16
  • 4
    Just an FYI: You want the revision-number of the commit that still has the file, **NOT** the one that removed it! – Lukas Knuth Jan 22 '16 at 16:23
  • 1
    if you just need the last version checked in you can do : $ hg revert No need to provide if you don't want to. – raddevus Mar 15 '21 at 20:13
8

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
mrucci
  • 4,342
  • 3
  • 33
  • 35
5

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.

PhoneixS
  • 10,574
  • 6
  • 57
  • 73
5

Well this worked for me.

hg revert -r revision pathToTheFile
Shweta
  • 924
  • 14
  • 23
1

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.

Community
  • 1
  • 1
keflavich
  • 18,278
  • 20
  • 86
  • 118
0

You can undo the last commit on a repo with hg rollback. There's only one level of rollback available, so if you did the remove with more than one commit, this won't completely undo your change. This only works on your local repository, so if you've pushed you won't be able to undo it in the remote repo.

ataylor
  • 64,891
  • 24
  • 161
  • 189
  • 1
    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. – PurplePilot Feb 02 '10 at 08:38
  • 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. – ataylor Feb 02 '10 at 19:01
  • 1
    `hg rollback` will not recover removed file because the deletion of the file is not part of the commit transaction. – mrucci Feb 03 '10 at 11:23
  • 4
    That's right. If you're looking to get at edits that were not committed before the remove, they are lost. – ataylor Feb 03 '10 at 23:42
0

The following worked for me.

hg revert -r <Revision Number> <File Name>

(Optional, to revert all files)

hg revert -r <Revision Number> --all
Naveen Kumar V
  • 2,559
  • 2
  • 29
  • 43
0

You can remove committed revisions using the hg strip command, which is provided by the mq (Mercurial Queues) extension. This should give you back your files.

Make a backup before trying that out, because it will alter Mercurial's database of changesets.

daniel kullmann
  • 13,653
  • 8
  • 51
  • 67
-1

The below method is straightforward and so stupid that it cannot go wrong. If you have deleted or renamed multiple files, it will be ok.

hg clone mydirectory mydirectory1

and now you start mc (or Far Manager) and compare what it was vs what it has become.

when it's done, just delete mydirectory1.

18446744073709551615
  • 16,368
  • 4
  • 94
  • 127