如何在 Mercurial 中恢复 1 个或多个已提交的文件,而不是整个变更集

发布于 2024-10-18 15:57:28 字数 569 浏览 5 评论 0原文

假设我有文件 File_AFile_BFile_C。我故意编辑了 File_AFile_B,但 File_C 我只是添加了一些我不打算提交的调试代码。然后我不小心做了一个

hg commit -m "comment"

How do just revert/rollback/backout File_C?基本上我很高兴能够继续,

hg update -r <oneRevBack> File_C
hg commit -m "put C back"

但更新不会使用过滤器 AFAIK,因此它也会恢复 File_AFile_B。我可以将 File_C 复制到某个地方,hg update,然后将其复制回来,但这似乎很蹩脚。有没有办法直接在 Mercurial 中执行此操作?

Say I have files File_A, File_B and File_C. I edit File_A and File_B on purpose but File_C I just added some debugging code that I don't plan to commit. Then by accident I do a

hg commit -m "comment"

How do just revert/rollback/backout File_C? Basically I'd be happy to be able to go

hg update -r <oneRevBack> File_C
hg commit -m "put C back"

but update doesn't take a filter AFAIK so it's also going to revert File_A and File_B. I can copy File_C somewhere, hg update, then copy it back but that seems lame. Is there a way to do this directly in Mercurial?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盗心人 2024-10-25 15:57:28

如果您使用 hg revert 而不是 hg update,您想要使用的确切命令就会起作用

hg revert -r .^ File_C
hg commit -m "put C back"

The exact command you wanted to use would have worked if you used hg revert instead of hg update

hg revert -r .^ File_C
hg commit -m "put C back"
無心 2024-10-25 15:57:28

在大多数情况下,VonC 的解决方案都是完美的。但是,如果无法选择回滚(例如,因为有问题的提交不是最后一次,或者因为您执行了复杂的部分提交),那么您可以使用对 File_C< 的不需要的更改的反向差异进行新的提交/code>:

hg diff -c REVISION --reverse File_C | hg import -m "Revert changes to File_C" -

其中 REVISION 指的是 File_C 意外提交的提交。第一个命令生成相关文件的反向差异。它的输出通过管道传输到 hg import (不要忘记尾随的 -)。

更新:您还可以使用backout命令:

hg backout -I File_C REVISION
hg commit -m "Revert changes of File_C in REVISION" File_C

backout命令会更改您的工作副本,因为它会撤消在rev中提交的对File_C的更改。 修订。之后,显式提交恢复。

In most cases VonC's solution is just perfect. However, if rollback is no option (e.g. because the commit in question is not the last one or because you did a complex partial commit), then you can do a new commit with the reversed diff of the unwanted changes to File_C:

hg diff -c REVISION --reverse File_C | hg import -m "Revert changes to File_C" -

Where REVISION refers to the commit where File_C has been committed accidentally. The first command produces the reversed diff for the file in question. It's output is piped to hg import (don't forget the trailing -).

UPDATE: You can also use the backout command:

hg backout -I File_C REVISION
hg commit -m "Revert changes of File_C in REVISION" File_C

The backout command changes your working copy in that it undoes the changes to File_C commited in rev. REVISION. Afterwards the revert is committed explicitly.

枕花眠 2024-10-25 15:57:28

您可以使用 hg rollback 撤消上次提交,然后再次重新执行,这次没有文件 C
(如 Mercurial 常见问题中所述,以及在此常见问题解答条目中指出“有回滚事务时没有备份”,所以谨慎使用)

You could use hg rollback to undo your last commit, and then re-do it again, this time without the file C.
(as mentioned in Mercurial FAQ, and also in this FAQ entry where it is noted that "there is no backup when you rollback a transaction", so use it with caution)

薆情海 2024-10-25 15:57:28
hg revert FILE -r REVISION

你可以通过以下方式将其恢复到上次提交

hg revert FILE -r .~1
hg revert FILE -r REVISION

you can revert it to last commit via

hg revert FILE -r .~1
不羁少年 2024-10-25 15:57:28

您还可以使用交互模式:

hg revert -i -r .^

选择您想要还原的内容并确认。

hg amend

You can also use interactive mode:

hg revert -i -r .^

Select what you want reverted and confirm.

hg amend
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文