回滚到 Mercurial 中的旧版本(如 git reset)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11322532/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rolling back to an old revision in Mercurial (like git reset)
提问by Sophie Alpert
Using git, I can throw away a mistaken or accidental commit using a command like
使用 git,我可以使用类似的命令丢弃错误或意外的提交
git reset --soft HEAD^
which resets the current branch (such as master
) to the current revision's parent commit HEAD^
without touching the working tree – you can replace --soft
with --hard
if you want to obliterate the working tree as well.
在不触及工作树的情况下将当前分支(例如master
)重置为当前修订的父提交HEAD^
-如果您也想删除工作树--soft
,--hard
则可以替换为。
In addition, if I want to get rid of more than one commit, I can use a command like
另外,如果我想摆脱多个提交,我可以使用像这样的命令
git reset --hard 53b94d0
which brings me back exactly to commit 53b94d0 as if I had never committed anything after that.
这让我完全回到了提交 53b94d0 的状态,就好像在那之后我从未提交过任何东西一样。
What's the Mercurial (hg) equivalent?
什么是 Mercurial (hg) 等价物?
回答by Sophie Alpert
Though Mercurial doesn't provide an easy alternative and shies away from the idea of mutable history, there are a few ways to achieve something close. In this post, I'll outline a few of them.
虽然 Mercurial 没有提供一个简单的替代方案,并且回避可变历史的想法,但有一些方法可以实现一些接近的目标。在这篇文章中,我将概述其中的一些。
hg rollback
汞回滚
If you specifically want to undo your last commit, you can use:
如果您特别想撤消上次提交,可以使用:
hg rollback
which will undo the commit (which doesn't touch the working tree – you should be able to follow it with a hg revert --all
or hg update -C
if you want to reset the entire working directory as well).
这将撤消提交(它不会触及工作树 - 你应该能够跟随它,hg revert --all
或者hg update -C
如果你想重置整个工作目录)。
hg clone -r
汞克隆 -r
Suppose you want to reset your repository called giraffe
to revision 77182fb7451f. If you cd
into the parent directory and run:
假设您想将您的存储库重置giraffe
为修订版 77182fb7451f。如果你cd
进入父目录并运行:
hg clone -r 77182fb7451f giraffe new-giraffe
cp giraffe/.hg/hgrc new-giraffe/.hg/
then you'll end up with new-giraffe, a repository at revision 77182fb7451f. (The cp
is necessary to ensure that new-giraffe paths
(like git's "remotes") correctly point to the origin repo instead of to the giraffe
folder as it would by default.)
那么你最终会得到 new-giraffe,一个版本为 77182fb7451f 的存储库。(这cp
是确保新长颈鹿paths
(如 git 的“遥控器”)正确指向原始存储库而不是giraffe
默认情况下的文件夹所必需的。)
This is faster than recloning from the internet because it only copies files locally on your disk (and on many systems, hard-links them to save even more time and space), but can still be really time-consuming if your repository is large.
这比从 Internet 重新克隆要快,因为它只复制本地磁盘上的文件(在许多系统上,硬链接它们以节省更多时间和空间),但如果您的存储库很大,仍然会非常耗时。
hg strip
汞带
If you want to do more complicated things with modifying the commit history (which is somewhat frowned on in the hg world, by the way), first enable the Mercurial Queues extensionby adding the following lines to your .hgrc
(there's no need to specify a path after the equals sign):
如果你想通过修改提交历史来做更复杂的事情(顺便说一下,这在 hg 世界中有点令人皱眉),首先通过添加以下几行来启用Mercurial Queues 扩展.hgrc
(不需要指定路径在等号之后):
[extensions]
mq =
This gives you (among other useful things) the hg strip
command to completely remove a changeset and all of its descendants from a repository. Thus you can use a command like:
这为您(以及其他有用的东西)提供了hg strip
从存储库中完全删除变更集及其所有后代的命令。因此,您可以使用如下命令:
hg strip 1cc72d33ea76
if 1cc72d33ea76 is the first "bad" changeset in your repository that you want to remove.
如果 1cc72d33ea76 是您要删除的存储库中的第一个“坏”变更集。
Unfortunately, it's often hard to remove exactly the right changesets using this method and it's all too easy to end up with multiple heads (which you can view using hg heads
), requiring tedious repeated application of hg strip
before you can get to where you want to be with no extra heads.
不幸的是,使用这种方法通常很难完全删除正确的变更集,而且很容易以多个头结束(您可以使用 来查看hg heads
),需要繁琐的重复应用,hg strip
然后才能到达您想要的位置而没有额外的头。
hg strip using revsets
使用 revsets 的 hg 条
Using hg revsetsyou can delete all the ancestors of .
(the current revision) which are notancestors of the revision you want to revert to, using a command like:
使用hg revsets,您可以使用以下命令删除.
(当前修订版)的所有祖先,这些祖先不是您要恢复到的修订的祖先:
hg strip "ancestors(.) and not ancestors(77182fb7451f)"
Make sure to first run:
确保首先运行:
hg log -r "ancestors(.) and not ancestors(77182fb7451f)"
which will show you all the changesets that hg strip
would remove, so you can make sure you don't irreparably harm your commit history (because you have backups… right?).
这将向您显示hg strip
将删除的所有变更集,因此您可以确保不会对提交历史造成不可挽回的损害(因为您有备份......对吗?)。