Mercurial 中有没有办法删除本地提交?

发布于 2024-08-22 20:32:25 字数 274 浏览 2 评论 0原文

所以我在 Mercurial 中不断犯一个愚蠢的错误。很多时候,我会在不执行“hg pull”和“hg update”的情况下开始工作。当我尝试推送更改时,出现错误。

有什么方法可以删除我的本地提交,这样我就可以避免创建多个头、分支等?我只想删除我的本地提交,将我的更改与提示合并,然后重新提交。听起来很简单,对吧?我似乎找不到任何方法来轻松删除本地提交,以便我可以干净地与提示合并。

再说一遍,我只是想删除使用“hg ci”进行的本地提交。我不想修改文件、恢复等。

So I keep making a silly mistake in Mercurial. Often times, I'll start work without doing an "hg pull" and an "hg update." When I try to push my changes, I get an error.

Is there any way to delete my local commits so I can avoid creating multiple heads, branches, etc? I just want to delete my local commits, merge my changes with the tip, and then re-commit. Sounds simple, right? I can't seem to find any way to easily delete local commits so I can cleanly merge with the tip.

Again, I'm only trying to delete local commits made with "hg ci". I don't want to modify files, revert, etc.

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

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

发布评论

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

评论(10

执着的年纪 2024-08-29 20:32:25

启用“strip”扩展并输入以下内容:

hg strip #changeset# --keep

其中 #changeset# 是要删除的变更集的哈希值。这将删除上述变更集包括从其继承的变更集并且不会影响您的工作目录。如果您还希望恢复提交的代码更改,请删除 --keep 选项。

有关详细信息,请查看剥离扩展

如果您收到“未知命令‘strip’”,您可能需要启用它。为此,找到 .hgrcMercurial.ini 文件并向其中添加以下内容:

[extensions]
strip =

请注意(正如 Juozas 在他的评论中提到的那样)拥有多个头是正常的工作流程善变的。您不应该使用 strip 命令来解决这个问题。相反,您应该将您的 head 与传入的 head 合并,解决所有冲突,测试,然后推送。

当您确实想要删除污染分支的变更集时,strip 命令非常有用。事实上,如果您处于此问题的情况,并且您想要完全删除所有“草稿”永久更改集,请查看最重要的答案,它基本上建议执行以下操作:

hg strip 'roots(outgoing())'

Enable the "strip" extension and type the following:

hg strip #changeset# --keep

Where #changeset# is the hash for the changeset you want to remove. This will remove the said changeset including changesets that descend from it and will leave your working directory untouched. If you wish to also revert your committed code changes remove the --keep option.

For more information, check the Strip Extension.

If you get "unkown command 'strip'" you may need to enable it. To do so find the .hgrc or Mercurial.ini file and add the following to it:

[extensions]
strip =

Note that (as Juozas mentioned in his comment) having multiple heads is normal workflow in Mercurial. You should not use the strip command to battle that. Instead, you should merge your head with the incoming head, resolve any conflicts, test, and then push.

The strip command is useful when you really want to get rid of changesets that pollute the branch. In fact, if you're in this question's situation and you want to completely remove all "draft" change sets permanently, check out the top answer, which basically suggests doing:

hg strip 'roots(outgoing())'
£冰雨忧蓝° 2024-08-29 20:32:25

如果您使用 Hg Tortoise,只需激活扩展程序“strip”:

  1. 文件/设置/扩展/
  2. 选择删除

然后选择要开始剥离的底部修订版,方法是右键单击,然后选择:

  1. 修改历史记录
  2. 剥离

就像这样:

在此处输入图像描述

在此示例中,它将删除从第 19 个修订版到最后提交的修订版(22)。

If you are using Hg Tortoise just activate the extension "strip" in:

  1. File/Settings/Extensions/
  2. Select strip

Then select the bottom revision from where you want to start striping, by doing right click on it, and selecting:

  1. Modify history
  2. Strip

Just like this:

enter image description here

In this example it will erase from the 19th revision to the last one commited(22).

做个少女永远怀春 2024-08-29 20:32:25

现代答案(仅在 Mercurial 2.1 之后相关):

使用 阶段 并标记修订版本您不想作为秘密(私人)共享。这样,当您推送时,它们就不会被发送。

在 TortoiseHG 中,您可以右键单击提交来更改其阶段。

另外:您还可以使用扩展“rebase”在拉取后将本地提交移至共享存储库的头部。

Modern answer (only relevant after Mercurial 2.1):

Use Phases and mark the revision(s) that you don't want to share as secret (private). That way when you push they won't get sent.

In TortoiseHG you can right click on a commit to change its phase.

Also: You can also use the extension "rebase" to move your local commits to the head of the shared repository after you pull.

呢古 2024-08-29 20:32:25

正如其他人指出的那样,您可能应该只是拉动然后合并头部,但是如果您真的想在没有任何 EditingHistory 工具,那么您只需 hg clone -r 您的存储库即可获取除这些更改之外的所有更改。

这不会将它们从原始存储库中删除,但会创建一个不包含它们的新克隆。然后您可以删除您修改的存储库(如果您愿意)。

As everyone else is pointing out you should probably just pull and then merge the heads, but if you really want to get rid of your commits without any of the EditingHistory tools then you can just hg clone -r your repo to get all but those changes.

This doesn't delete them from the original repository, but it creates a new clone that doesn't have them. Then you can delete the repo you modified (if you'd like).

牵你的手,一向走下去 2024-08-29 20:32:25

我也遇到过这个问题。我进行了 2 次提交,并希望回滚并删除这两次提交。

$ hg rollback

但是 hg rollback 只是回滚到最后一次提交,而不是两次提交。当时我没有意识到这一点,我改变了代码。

当我发现 hg rollback 刚刚回滚了一次提交时,我发现我可以使用 hg strip #changeset#。因此,我使用 hg log -l 10 查找最新的 10 个提交,并获取我想要strip 的正确变更集。

$ hg log -l 10
changeset:   2499:81a7a8f7a5cd
branch:      component_engine
tag:         tip
user:        myname<[email protected]>
date:        Fri Aug 14 12:22:02 2015 +0800
summary:     get runs from sandbox

changeset:   2498:9e3e1de76127
branch:      component_engine
user:        other_user_name<[email protected]>
date:        Mon Aug 03 09:50:18 2015 +0800
summary:     Set current destination to a copy incoming exchange

......

$ hg strip 2499
abort: local changes found

中止:发现本地更改是什么意思?这意味着 hg 发现了尚未提交的代码更改。因此,要解决此问题,您应该 hg diff 保存已更改的代码,并 hg reverthg strip #changeset#。就像这样:

$ hg diff > /PATH/TO/SAVE/YOUR/DIFF/FILE/my.diff
$ hg revert file_you_have_changed
$ hg strip #changeset#

完成上述操作后,您可以修补 diff 文件,并且您的代码可以添加回您的项目。

$ patch -p1 < /PATH/TO/SAVE/YOUR/DIFF/FILE/my.diff

I came across this problem too. I made 2 commit and wanted to rollback and delete both commits.

$ hg rollback

But hg rollback just rolls back to the last commit, not the 2 commits. At that time I did not realize this and I changed the code.

When I found hg rollback had just rolled back one commit, I found I could use hg strip #changeset#. So, I used hg log -l 10 to find the latest 10 commits and get the right changeset I wanted to strip.

$ hg log -l 10
changeset:   2499:81a7a8f7a5cd
branch:      component_engine
tag:         tip
user:        myname<[email protected]>
date:        Fri Aug 14 12:22:02 2015 +0800
summary:     get runs from sandbox

changeset:   2498:9e3e1de76127
branch:      component_engine
user:        other_user_name<[email protected]>
date:        Mon Aug 03 09:50:18 2015 +0800
summary:     Set current destination to a copy incoming exchange

......

$ hg strip 2499
abort: local changes found

What does abort: local changes found mean? It means that hg found changes to the code that haven't been committed yet. So, to solve this, you should hg diff to save the code you have changed and hg revert and hg strip #changeset#. Just like this:

$ hg diff > /PATH/TO/SAVE/YOUR/DIFF/FILE/my.diff
$ hg revert file_you_have_changed
$ hg strip #changeset#

After you have done the above, you can patch the diff file and your code can be added back to your project.

$ patch -p1 < /PATH/TO/SAVE/YOUR/DIFF/FILE/my.diff
农村范ル 2024-08-29 20:32:25

您可以使用 Rebase 扩展 更轻松地解决此问题,只需使用 hg pull --rebase 并且您的提交会自动重新提交到拉取的修订版,从而避免分支问题。

You can get around this even more easily with the Rebase extension, just use hg pull --rebase and your commits are automatically re-comitted to the pulled revision, avoiding the branching issue.

携余温的黄昏 2024-08-29 20:32:25

hg strip 就是您正在寻找的。如果您熟悉 git,它类似于 git reset。

使用控制台:

  1. 您需要知道修订号。 hg log -l 10。此命令显示最近 10 次提交。找到您正在寻找的提交。您需要来自变更集行的 4 位数字 changeset: 5888:ba6205914681

  2. 然后hg strip -r 5888 --keep。这将删除提交记录,但保留所有已修改的文件,然后您可以重新提交它们。
    (如果您想删除文件,只需删除 --keep hg strip -r 5888

hg strip is what you are looking for. It's analogous of git reset if you familiar with git.

Use console:

  1. You need to know the revision number. hg log -l 10. This command shows the last 10 commits. Find commit you are looking for. You need 4 digit number from changeset line changeset: 5888:ba6205914681

  2. Then hg strip -r 5888 --keep. This removes the record of the commit but keeps all files modified and then you could recommit them.
    (if you want to delete files to just remove --keep hg strip -r 5888

夏日浅笑〃 2024-08-29 20:32:25

如果您熟悉 git,您会很乐意使用 histedit ,其工作方式类似于 <代码>git rebase -i。

If you are familiar with git you'll be happy to use histedit that works like git rebase -i.

八巷 2024-08-29 20:32:25

[汞乌龟4.6.1]
如果是最近的操作,您可以使用“回滚/撤消”操作 (Ctrl+U)。

HG 乌龟图片

[Hg Tortoise 4.6.1]
If it's recent action, you can use "Rollback/Undo" action (Ctrl+U).

HG Tortoise Image

叹倦 2024-08-29 20:32:25

除了 Samaursa 的出色答案之外,您还可以使用 evolve< /code>扩展的 prune 作为 strip 的安全且可恢复的版本,可以让您在做错任何事情时返回。

我的 .hgrc 上有这些别名:

 # Prunes all draft changesets on the current repository
 reset-tree = prune -r "outgoing() and not obsolete()"
 # *STRIPS* all draft changesets on current repository. This deletes history.
 force-reset-tree = strip 'roots(outgoing())'

请注意,prune 也有 --keep,就像 strip 一样,保持工作目录完整,允许您重新提交文件。

In addition to Samaursa's excelent answer, you can use the evolve extension's prune as a safe and recoverable version of strip that will allow you to go back in case you do anything wrong.

I have these alias on my .hgrc:

 # Prunes all draft changesets on the current repository
 reset-tree = prune -r "outgoing() and not obsolete()"
 # *STRIPS* all draft changesets on current repository. This deletes history.
 force-reset-tree = strip 'roots(outgoing())'

Note that prune also has --keep, just like strip, to keep the working directory intact allowing you to recommit the files.

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