如何在 Mercurial 中编辑错误的提交消息?
我目前正在使用 TortoiseHg (Mercurial) 并意外提交了错误的提交消息。 如何在存储库中编辑此提交消息?
I am currently using TortoiseHg (Mercurial) and accidentally committed an incorrect commit message. How do I go about editing this commit message in the repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
更新: Mercurial 添加了
--amend
,它应该是现在的首选选项< /a>.您可以使用
hg rollback
回滚最后一次提交(但仅限最后一次),然后重新应用它。重要:这永久删除最新的提交(或拉取)。 因此,如果您进行了
hg update
且提交不再位于您的工作目录中,那么它就会永远消失。 所以先制作一个副本。除此之外,您无法更改存储库的历史记录(包括提交消息),因为其中的所有内容都经过校验和。 您唯一可以做的就是在给定的变更集之后修剪历史记录,然后相应地重新创建它。
如果您已经发布了更改(除非您可以获取所有副本),那么所有这些都不起作用,并且您也无法“重写历史记录”,其中包括(由其他人)GPG 签名的提交。
Update: Mercurial has added
--amend
which should be the preferred option now.You can rollback the last commit (but only the last one) with
hg rollback
and then reapply it.Important: this permanently removes the latest commit (or pull). So if you've done a
hg update
that commit is no longer in your working directory then it's gone forever. So make a copy first.Other than that, you cannot change the repository's history (including commit messages), because everything in there is check-summed. The only thing you could do is prune the history after a given changeset, and then recreate it accordingly.
None of this will work if you have already published your changes (unless you can get hold of all copies), and you also cannot "rewrite history" that include GPG-signed commits (by other people).
好吧,我以前是这样做的:
想象一下,你有 500 次提交,而你的错误提交消息位于 r.498 中。
Well, I used to do this way:
Imagine, you have 500 commits, and your erroneous commit message is in r.498.
好消息:hg 2.2 刚刚添加 git 类似
--amend
选项。在 tortoiseHg 中,您可以通过选择提交按钮右侧的黑色箭头
来使用“修改当前版本”
Good news: hg 2.2 just added git like
--amend
option.and in tortoiseHg, you can use "Amend current revision" by select black arrow on the right of commit button
我知道这是一篇旧帖子,您将问题标记为已回答。 我最近在寻找同样的东西,我发现
histedit
扩展非常有用。 该过程的解释如下:http://knowledgestockpile .blogspot.com/2010/12/changing-commit-message-of-revision-in.html
I know this is an old post and you marked the question as answered. I was looking for the same thing recently and I found the
histedit
extension very useful. The process is explained here:http://knowledgestockpile.blogspot.com/2010/12/changing-commit-message-of-revision-in.html
最后一个操作是有问题的提交
要在最后一个 Mercurial 操作是提交时更改最后一次提交的提交消息,您可以使用
回滚最后一次提交并使用新消息重新提交它:
但要小心,因为回滚命令还回滚以下操作:
(请参阅
hg help rollback
)因此,如果您不确定最后一个mercurial命令是否是
hg ci
>,不要使用hg rollback
。更改任何其他提交消息
您可以使用 mq 扩展< /a>,随 Mercurial 一起分发,用于更改任何提交的提交消息。
仅当公共中尚不存在包含要重命名的变更集的克隆存储库时,此方法才有用,因为这样做会更改它以及所有后续变更集的变更集哈希。
这意味着您必须能够删除包含要重命名的变更集的所有现有克隆,否则在它们之间推/拉将不起作用。
要使用 mq 扩展,您必须显式启用它,例如在 UNIX 下检查您的
~/.hgrc
,其中应包含以下行:假设您要更改版本 X - 首先
qimport< /code> 导入修订版 X 及以下版本。 现在它们被注册为一堆已应用的补丁。 弹出 (
qpop
) 除 X 之外的完整堆栈,使 X 可通过qrefresh
进行更改。 提交消息更改后,您必须再次推送所有补丁(qpop
)以重新应用它们,即重新创建以下修订。 补丁堆栈不需要任何内容,因此可以通过 qfinish 删除它。以下演示脚本显示了所有正在执行的操作。 在示例中,第三个变更集的提交消息被重命名。
将其复制到空目录并执行它,例如通过:
输出应包括原始变更集消息:
以及重命名操作更改的消息:(
使用 Mercurial 4.5.2 进行测试)
Last operation was the commit in question
To change the commit message of the last commit when the last mercurial operation was a commit you can use
to roll back the last commit and re-commit it with the new message:
But be careful because the rollback command also rolls back following operations:
(see
hg help rollback
)Thus, if you are not sure if the last mercurial command was a
hg ci
, don't usehg rollback
.Change any other commit message
You can use the mq extension, which is distributed with Mercurial, to change the commit message of any commit.
This approach is only useful when there aren't already cloned repositories in the public that contain the changeset you want to rename because doing so alters the changeset hash of it and all following changesets.
That means that you have to be able to remove all existing clones that include the changeset you want to rename, or else pushing/pulling between them wouldn't work.
To use the mq extension you have to explicitly enable it, e.g. under UNIX check your
~/.hgrc
, which should contain following lines:Say that you want to change revision X - first
qimport
imports revisions X and following. Now they are registered as a stack of applied patches. Popping (qpop
) the complete stack except X makes X available for changes viaqrefresh
. After the commit message is changed you have to push all patches again (qpop
) to re-apply them, i.e. to recreate the following revisions. The stack of patches isn't needed any, thus it can be removed viaqfinish
.Following demo script shows all operations in action. In the example the commit message of third changeset is renamed.
Copy it to an empty directory an execute it e.g. via:
The output should include the original changeset message:
And the rename operation the changed message:
(Tested with Mercurial 4.5.2)
在 TortoiseHg 中,右键单击要修改的修订版本。 选择修改历史记录->导入 MQ。 这会将 Mercurial 变更集中的所有修订(包括选定修订)转换为 Mercurial 队列补丁。 选择您要修改消息的补丁,它应该会自动将屏幕更改为 MQ 编辑器。 编辑屏幕中间的消息,然后单击 QRefresh。 最后,右键单击补丁并选择“修改历史记录”->“完成补丁”,这会将其从补丁转换回更改集。
哦,这假设 MQ 是此存储库上 TortoiseHG 的活动扩展。 如果没有,您应该能够单击“文件”->“设置”,单击“扩展”,然后单击“mq”复选框。 它应该警告您必须在扩展程序激活之前关闭 TortoiseHg,因此关闭并重新打开。
In TortoiseHg, right-click on the revision you want to modify. Choose Modify History->Import MQ. That will convert all the revisions up to and including the selected revision from Mercurial changesets into Mercurial Queue patches. Select the Patch you want to modify the message for, and it should automatically change the screen to the MQ editor. Edit the message which is in the middle of the screen, then click QRefresh. Finally, right click on the patch and choose Modify History->Finish Patch, which will convert it from a patch back into a change set.
Oh, this assumes that MQ is an active extension for TortoiseHG on this repository. If not, you should be able to click File->Settings, click Extensions, and click the mq checkbox. It should warn you that you have to close TortoiseHg before the extension is active, so close and reopen.
编辑:正如用户所指出的,不要使用MQ,使用
commit --amend
。 这个答案现在最具有历史意义。正如其他人提到的,MQ 扩展更适合此任务,并且您不会冒破坏工作的风险。 为此:
通过将类似内容添加到 hgrc 来启用 MQ 扩展:
<前><代码>[扩展名]
MQ =
更新到您要编辑的变更集,通常提示:
将当前变更集导入队列:
<前><代码>hg qimport -r 。
刷新补丁,并编辑提交消息:
<前><代码>hg qrefresh -e
完成所有应用的补丁(在本例中为一个)并将它们存储为常规变更集:
<前><代码>hg qfinish -a
我不熟悉 TortoiseHg,但命令应该与上面的类似。 我还认为值得一提的是,编辑历史是有风险的; 仅当您绝对确定变更集没有被推送到其他地方或从其他任何地方拉取时才应该这样做。
EDIT: As pointed out by users, don't use MQ, use
commit --amend
. This answer is mostly of historic interest now.As others have mentioned the MQ extension is much more suited for this task, and you don't run the risk of destroying your work. To do this:
Enable the MQ extension, by adding something like this to your hgrc:
Update to the changeset you want to edit, typically tip:
Import the current changeset into the queue:
Refresh the patch, and edit the commit message:
Finish all applied patches (one, in this case) and store them as regular changesets:
I'm not familiar with TortoiseHg, but the commands should be similar to those above. I also believe it's worth mentioning that editing history is risky; you should only do it if you're absolutely certain that the changeset hasn't been pushed to or pulled from anywhere else.
回滚并重新应用确实是简单的解决方案,但它只能对最后一次提交有帮助。 Mercurial 队列功能更强大(请注意,您需要启用 Mercurial 队列扩展才能使用“hg q*”命令)。
Rollback-and-reapply is realy simple solution, but it can help only with the last commit. Mercurial Queues is much more powerful thing (note that you need to enable Mercurial Queues Extension in order to use "hg q*" commands).
我就是这样做的。 首先,不要推动你的改变,否则你就不走运了。 获取并安装 collapse 扩展程序。 提交另一个虚拟变更集。 然后使用塌陷将前两个变更集合并为一个。 它将提示您输入新的提交消息,并为您提供已有的消息作为起点。 您已经有效地更改了原始提交消息。
I did it this way. Firstly, don't push your changes or you are out of luck. Grab and install the collapse extension. Commit another dummy changeset. Then use collapse to combine the previous two changesets into one. It will prompt you for a new commit message, giving you the messages that you already have as a starting point. You have effectively changed your original commit message.
如果我要编辑的版本不太旧,我会使用一种技巧:
假设您的版本为 500,并且想要编辑 497。
编辑 rev497 文件并更改消息。 (它位于第一行之后,前面有“#”)
One hack i use if the revision i want to edit is not so old:
Let's say you're at rev 500 and you want to edit 497.
Edit rev497 file and change the message. (It's after first lines preceded by "#")
还有另一种使用 MQ 扩展和 调试命令< /a>. 这是修改历史记录而不丢失数据的通用方法。 让我假设与 Antonio 相同的情况。
There is another approach with the MQ extension and the debug commands. This is a general way to modify history without losing data. Let me assume the same situation as Antonio.
上面讨论中的一个小亮点 - 感谢@Codest 和@Kevin Pullin。
在 TortoiseHg 中,提交按钮旁边有一个下拉选项。 选择“修改当前版本”会返回注释和文件列表。 太有用了。
A little gem in the discussion above - thanks to @Codest and @Kevin Pullin.
In TortoiseHg, there's a dropdown option adjacent to the commit button. Selecting "Amend current revision" brings back the comment and the list of files. SO useful.