如何在 Mercurial 中恢复已删除的文件(如果有的话)?
意外地,通过使用 GUI 而不是 CLI,我删除了 Mercurial 项目中的每个文件。
我使用“恢复”恢复正常,但丢失了一些工作,因为我有时间机器,所以我可以轻松恢复这些工作。但是有没有办法取消删除/取消删除此类文件?翻遍了手册并用谷歌搜索,但看不到任何东西。有插件吗?
我可能在这里回答我自己的问题,但文件已从目录中消失,并且没有在垃圾箱中恢复,所以我假设删除是不可撤销的?
ps 我知道 hgforget
或 hgremove-Af
将删除而不从目录中删除,但我的问题与我犯的错误有关,而不是冷静地思考该操作通过。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
首先,使用
hg grep
查找您想要恢复的已删除文件。此命令的输出将显示该文件存在的最后版本以及已删除文件的路径。其次,运行
hg revert -r; <已删除文件的路径>
删除的文件现在将位于您的工作副本中,准备好重新提交到 head 中。
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.
引用评论:
如果是这种情况,那么您只需将工作目录更新到以前的版本:
注意负版本号。如果您的文件删除的内容不在之前的版本中,您可以使用以下命令找到它们:
Quote from comment:
If this is the case then you just need to update the working directory to the previous revision:
Note the negative revision number. If the files you deleted aren't in the previous revision, you can find them by using:
对于 Mercurial 1.6 及更高版本
如果您知道删除文件的名称,您可以轻松找到其修订版本:
这将为您提供删除名为 NAME.c(在根目录中)的文件的修订版本。
然后您可以将文件恢复到以前的版本(像其他答案一样):
您可以使用文件名模式来适应您所知道的内容,例如您可以使用
**/NAME.c
在所有目录中搜索。您可以在文件名模式中阅读相关内容。并使用此链接了解新的revset 规范 。For Mercurial 1.6 and above
If you know the name of the delete file you can find its revision easily with:
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):
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 addremove
,这根本不是我的意图,因此必须撤消所有这些删除。使用快速查找 Mercurial 存储库历史记录中已删除的文件? + xargs + tr,将所有修订版 -3 删除内容恢复为修订版 -4 中的版本:
请注意,如果您的任何文件名称中包含空格,此操作将会失败; http://hgbook.red-bean.com/ read/customizing-the-output-of-mercurial.html 似乎没有任何模板,其中
{file_dels}
在\n
处被分割片刻。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:
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.您可以使用
hg rollback
撤消存储库上的最后一次提交。只有一级回滚可用,因此如果您通过多次提交进行删除,这不会完全撤消您的更改。这仅适用于您的本地存储库,因此如果您已推送,您将无法在远程存储库中撤消它。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.您可以使用 mq (Mercurial Queues) 扩展提供的
hg strip
命令删除已提交的修订。这应该会返回你的文件。在尝试之前进行备份,因为它会改变 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.
以下内容对我有用。
(可选,恢复所有文件)
The following worked for me.
(Optional, to revert all files)
下面的方法既简单又愚蠢,不会出错。
如果你删除或重命名了多个文件,那就没问题了。
现在您启动 mc(或 Far Manager)并比较它的过去和现在的情况。
完成后,只需删除
mydirectory1
。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.
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
.