如何在 Mercurial 中恢复已删除的文件(如果有的话) ?

偶然地,通过使用 GUI 而不是 CLI,我删除了 Mercurial 项目中的每个文件。

我用“回复”恢复了,还丢失了一些工作,因为我有时间机器,我可以很容易地恢复。但是有没有办法取消删除这些文件呢?我翻遍了手册,谷歌了一下,但是什么都看不到。有插件吗?

我可能在这里回答我自己的问题,但是文件已经从目录中消失了,并且没有在垃圾桶中恢复,所以我假设“移除”是不可撤销的?

我知道 hg forget或者 hg remove -Af会删除而不会从目录中删除,但是我的问题与我犯的错误有关,而不是冷静地思考这个动作。

52838 次浏览

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.

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

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.

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.

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.

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.

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.

Well this worked for me.

hg revert -r revision pathToTheFile

The following worked for me.

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

(Optional, to revert all files)

hg revert -r <Revision Number> --all