如何在 Mercurial 中编辑错误的提交消息?

发布于 2024-07-15 06:18:50 字数 69 浏览 11 评论 0原文

我目前正在使用 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 技术交流群。

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

发布评论

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

评论(12

殤城〤 2024-07-22 06:18:50

更新: 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).

指尖上的星空 2024-07-22 06:18:50

好吧,我以前是这样做的:

想象一下,你有 500 次提交,而你的错误提交消息位于 r.498 中。

hg qimport -r 498:tip
hg qpop -a
joe .hg/patches/498.diff
(change the comment, after the mercurial header)
hg qpush -a
hg qdelete -r qbase:qtip

Well, I used to do this way:

Imagine, you have 500 commits, and your erroneous commit message is in r.498.

hg qimport -r 498:tip
hg qpop -a
joe .hg/patches/498.diff
(change the comment, after the mercurial header)
hg qpush -a
hg qdelete -r qbase:qtip
甜中书 2024-07-22 06:18:50

好消息:hg 2.2 刚刚添加 git 类似 --amend 选项。

在 tortoiseHg 中,您可以通过选择提交按钮右侧的黑色箭头

a来使用“修改当前版本”

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

a

关于从前 2024-07-22 06:18:50

我知道这是一篇旧帖子,您将问题标记为已回答。 我最近在寻找同样的东西,我发现 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

静待花开 2024-07-22 06:18:50

最后一个操作是有问题的提交

要在最后一个 Mercurial 操作是提交时更改最后一次提交的提交消息,您可以使用

$ hg rollback

回滚最后一次提交并使用新消息重新提交它:

$ hg ci -m 'new message'

但要小心,因为回滚命令还回滚以下操作:

  • 导入
    • 拉动
    • 推送(将此存储库作为目标)
    • 分拆

(请参阅hg help rollback

因此,如果您不确定最后一个mercurial命令是否是hg ci >,不要使用hg rollback

更改任何其他提交消息

您可以使用 mq 扩展< /a>,随 Mercurial 一起分发,用于更改任何提交的提交消息。

仅当公共中尚不存在包含要重命名的变更集的克隆存储库时,此方法才有用,因为这样做会更改它以及所有后续变更集的变更集哈希。

这意味着您必须能够删除包含要重命名的变更集的所有现有克隆,否则在它们之间推/拉将不起作用。

要使用 mq 扩展,您必须显式启用它,例如在 UNIX 下检查您的 ~/.hgrc,其中应包含以下行:

[extensions]
mq=

假设您要更改版本 X - 首先 qimport< /code> 导入修订版 X 及以下版本。 现在它们被注册为一堆已应用的补丁。 弹出 (qpop) 除 X 之外的完整堆栈,使 X 可通过 qrefresh 进行更改。 提交消息更改后,您必须再次推送所有补丁(qpop)以重新应用它们,即重新创建以下修订。 补丁堆栈不需要任何内容​​,因此可以通过 qfinish 删除它。

以下演示脚本显示了所有正在执行的操作。 在示例中,第三个变更集的提交消息被重命名。

# test.sh
cd $(dirname $0)
set -x -e -u
echo INFO: Delete old stuff
rm -rf .hg `seq 5`
echo INFO: Setup repository with 5 revisions
hg init
echo '[ui]' > .hg/hgrc
echo 'username=Joe User <[email protected]>' >> .hg/hgrc
echo 'style = compact' >> .hg/hgrc
echo '[extensions]' >> .hg/hgrc
echo 'mq=' >> .hg/hgrc
for i in `seq 5`; do
  touch $i && hg add $i && hg ci -m "changeset message $i" $i
done
hg log 
echo INFO: Need to rename the commit message on the 3rd revision
echo INFO: Displays all patches
hg qseries
echo INFO: Import all revisions including the 3rd to the last one as patches
hg qimport -r $(hg identify -n -r 'children(2)'):tip
hg qseries
echo INFO: Pop patches
hg qpop -a
hg qseries
hg log 
hg parent
hg commit --amend -m 'CHANGED MESSAGE'
hg log 
echo INFO: Push all remaining patches
hg qpush -a
hg log 
hg qseries
echo INFO: Remove all patches
hg qfinish -a
hg qseries && hg log && hg parent

将其复制到空目录并执行它,例如通过:

$ bash test.sh 2>&1 | tee log

输出应包括原始变更集消息:

+ hg log
[..]
2   53bc13f21b04   2011-08-31 17:26 +0200   juser
  changeset message 3

以及重命名操作更改的消息:(

+ hg log
[..]
2   3ff8a832d057   2011-08-31 17:26 +0200   juser
  CHANGED MESSAGE

使用 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

$ hg rollback

to roll back the last commit and re-commit it with the new message:

$ hg ci -m 'new message'

But be careful because the rollback command also rolls back following operations:

  • import
    • pull
    • push (with this repository as the destination)
    • unbundle

(see hg help rollback)

Thus, if you are not sure if the last mercurial command was a hg ci, don't use hg 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:

[extensions]
mq=

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 via qrefresh. 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 via qfinish.

Following demo script shows all operations in action. In the example the commit message of third changeset is renamed.

# test.sh
cd $(dirname $0)
set -x -e -u
echo INFO: Delete old stuff
rm -rf .hg `seq 5`
echo INFO: Setup repository with 5 revisions
hg init
echo '[ui]' > .hg/hgrc
echo 'username=Joe User <[email protected]>' >> .hg/hgrc
echo 'style = compact' >> .hg/hgrc
echo '[extensions]' >> .hg/hgrc
echo 'mq=' >> .hg/hgrc
for i in `seq 5`; do
  touch $i && hg add $i && hg ci -m "changeset message $i" $i
done
hg log 
echo INFO: Need to rename the commit message on the 3rd revision
echo INFO: Displays all patches
hg qseries
echo INFO: Import all revisions including the 3rd to the last one as patches
hg qimport -r $(hg identify -n -r 'children(2)'):tip
hg qseries
echo INFO: Pop patches
hg qpop -a
hg qseries
hg log 
hg parent
hg commit --amend -m 'CHANGED MESSAGE'
hg log 
echo INFO: Push all remaining patches
hg qpush -a
hg log 
hg qseries
echo INFO: Remove all patches
hg qfinish -a
hg qseries && hg log && hg parent

Copy it to an empty directory an execute it e.g. via:

$ bash test.sh 2>&1 | tee log

The output should include the original changeset message:

+ hg log
[..]
2   53bc13f21b04   2011-08-31 17:26 +0200   juser
  changeset message 3

And the rename operation the changed message:

+ hg log
[..]
2   3ff8a832d057   2011-08-31 17:26 +0200   juser
  CHANGED MESSAGE

(Tested with Mercurial 4.5.2)

唐婉 2024-07-22 06:18:50

在 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.

温柔戏命师 2024-07-22 06:18:50

编辑:正如用户所指出的,不要使用MQ,使用commit --amend。 这个答案现在最具有历史意义。

正如其他人提到的,MQ 扩展更适合此任务,并且您不会冒破坏工作的风险。 为此:

  1. 通过将类似内容添加到 hgrc 来启用 MQ 扩展:

    <前><代码>[扩展名]
    MQ =

  2. 更新到您要编辑的变更集,通常提示:

    hg 增加 $rev 
      
  3. 将当前变更集导入队列:

    <前><代码>hg qimport -r 。

  4. 刷新补丁,并编辑提交消息:

    <前><代码>hg qrefresh -e

  5. 完成所有应用的补丁(在本例中为一个)并将它们存储为常规变更集:

    <前><代码>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:

  1. Enable the MQ extension, by adding something like this to your hgrc:

    [extensions]
    mq =
    
  2. Update to the changeset you want to edit, typically tip:

    hg up $rev
    
  3. Import the current changeset into the queue:

    hg qimport -r .
    
  4. Refresh the patch, and edit the commit message:

    hg qrefresh -e
    
  5. Finish all applied patches (one, in this case) and store them as regular changesets:

    hg qfinish -a
    

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.

后知后觉 2024-07-22 06:18:50

回滚并重新应用确实是简单的解决方案,但它只能对最后一次提交有帮助。 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).

失与倦" 2024-07-22 06:18:50

我就是这样做的。 首先,不要推动你的改变,否则你就不走运了。 获取并安装 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.

情未る 2024-07-22 06:18:50

如果我要编辑的版本不太旧,我会使用一种技巧:

假设您的版本为 500,并且想要编辑 497。

hg export -o rev497 497
hg export -o rev498 498
hg export -o rev499 499
hg export -o rev500 500

编辑 rev497 文件并更改消息。 (它位于第一行之后,前面有“#”)

hg import rev497
hg import rev498
hg import rev499
hg import rev500

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.

hg export -o rev497 497
hg export -o rev498 498
hg export -o rev499 499
hg export -o rev500 500

Edit rev497 file and change the message. (It's after first lines preceded by "#")

hg import rev497
hg import rev498
hg import rev499
hg import rev500
醉南桥 2024-07-22 06:18:50

还有另一种使用 MQ 扩展和 调试命令< /a>. 这是修改历史记录而不丢失数据的通用方法。 让我假设与 Antonio 相同的情况。

// set current tip to rev 497
hg debugsetparents 497
hg debugrebuildstate
// hg add/remove if needed
hg commit
hg strip [-n] 498

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.

// set current tip to rev 497
hg debugsetparents 497
hg debugrebuildstate
// hg add/remove if needed
hg commit
hg strip [-n] 498
二智少女猫性小仙女 2024-07-22 06:18:50

上面讨论中的一个小亮点 - 感谢@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.

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