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

Mercurial

Mercurial Problem Overview


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.

Mercurial Solutions


Solution 1 - Mercurial

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 - Mercurial

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 - Mercurial

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 4 - Mercurial

Well this worked for me.

hg revert -r revision pathToTheFile

Solution 5 - Mercurial

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 https://stackoverflow.com/questions/1013550/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.

Solution 6 - Mercurial

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.

Solution 7 - Mercurial

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.

Solution 8 - Mercurial

The following worked for me.

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

(Optional, to revert all files)

hg revert -r <Revision Number> --all

Solution 9 - Mercurial

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPurplePilotView Question on Stackoverflow
Solution 1 - MercurialBungleFeetView Answer on Stackoverflow
Solution 2 - MercurialmrucciView Answer on Stackoverflow
Solution 3 - MercurialPhoneixSView Answer on Stackoverflow
Solution 4 - MercurialShwetaView Answer on Stackoverflow
Solution 5 - MercurialkeflavichView Answer on Stackoverflow
Solution 6 - MercurialataylorView Answer on Stackoverflow
Solution 7 - Mercurialdaniel kullmannView Answer on Stackoverflow
Solution 8 - MercurialNaveen Kumar VView Answer on Stackoverflow
Solution 9 - Mercurial18446744073709551615View Answer on Stackoverflow