恢复 git 中的一系列提交
如何恢复 git 中的一系列提交?从查看 gitrevisions 文档,我看不出如何指定我需要的范围。例如:
A -> B -> C -> D -> E -> HEAD
我想做相当于:
git revert B-D
其中结果将是:
A -> B -> C -> D -> E -> F -> HEAD
其中 F 包含 BD 的相反内容。
How can I revert a range of commits in git? From looking at the gitrevisions documentation, I cannot see how to specify the range I need. For example:
A -> B -> C -> D -> E -> HEAD
I want to do the equivalent of:
git revert B-D
where the result would be:
A -> B -> C -> D -> E -> F -> HEAD
where F contains the reverse of B-D inclusive.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您使用什么版本的 Git?
仅在 Git1.7.2+ 中支持恢复多个提交:请参阅“使用多次恢复回滚到旧的提交。”了解更多详细信息。
当前的
git revert
手册页 仅适用于当前 Git 版本(1.7.4+)。正如 OP Alex Spurling 在评论中报告的那样:
升级到 1.7.4 效果很好。
为了回答我自己的问题,这是我正在寻找的语法:
B^
表示“B 的第一个父提交”:允许在还原中包含B
。请参阅“
git rev-parse
指定修订版部分”其中包括^
,例如HEAD^
语法:请参阅“插入符号 (^
) 字符的意思是?")请注意,每个恢复的提交都是单独提交的。
Henrik N 在 评论:
如下所示,您可以在不立即提交的情况下恢复:
What version of Git are you using?
Reverting multiple commits in only supported in Git1.7.2+: see "Rollback to an old commit using revert multiple times." for more details.
The current
git revert
man page is only for the current Git version (1.7.4+).As the OP Alex Spurling reports in the comments:
Upgrading to 1.7.4 works fine.
To answer my own question, this is the syntax I was looking for:
B^
means "the first parent commit of B": that allows to includeB
in the revert.See "
git rev-parse
SPECIFYING REVISIONS section" which include the<rev>^
, e.g.HEAD^
syntax: see more at "What does the caret (^
) character mean?")Note that each reverted commit is committed separately.
Henrik N clarifies in the comments:
As shown below, you can revert without committing right away:
如何通过一次恢复提交来恢复一系列提交
如果您想在一次提交中恢复提交范围 B 到 D(至少在 git 版本 2 中),您可以可以这样做:
-n
(或--no-commit
)参数告诉 git 恢复从 B 的父提交(排除)到 D 的提交所做的更改提交(包括),但不创建任何包含已恢复更改的提交。恢复仅修改工作树(您的活动文件系统)和索引(您的暂存文件部分)。不要忘记在运行 git revert -n 后提交更改:
您还可以使用相同的方法在一次提交中还原多个不相关的提交。例如:要恢复 B 和 D,但不恢复 C:
参考文献:
https://www.kernel.org/pub/software/scm/git/docs/git-revert.html
感谢 Honza Haering 更正。
来自
man git revert
:<块引用>
-n
,--no-commit
通常该命令会自动创建一些提交,并带有提交日志消息,说明哪些提交已恢复。此标志应用将命名提交恢复到工作树和索引所需的更改,但不进行提交。此外,使用此选项时,您的索引不必与
HEAD
提交匹配。恢复是根据索引的开始状态完成的。当连续将多个提交的效果恢复到索引时,这非常有用。
How to revert a range of commits with one single revert commit
If you want to revert commit range B to D (at least in git version 2) in a single commit, you can do:
The
-n
(or--no-commit
) argument tells git to revert the changes done by the commits starting from B's parent commit (excluded) to the D commit (included), but not to create any commit with the reverted changes. The revert only modifies the working tree (your active file system) and the index (your staged file section).Don't forget to commit the changes after running
git revert -n
:You can also revert multiple unrelated commits in a single commit, using the same method. For example: to revert B and D but not C:
References:
https://www.kernel.org/pub/software/scm/git/docs/git-revert.html
Thanks Honza Haering for the correction.
From
man git revert
:执行
git revert OLDER_COMMIT^..NEWER_COMMIT
对我来说不起作用。我使用了 git revert -n OLDER_COMMIT^..NEWER_COMMIT ,一切都很好。我使用的是 git 版本
1.7.9.6
。Doing
git revert OLDER_COMMIT^..NEWER_COMMIT
didn't work for me.I used
git revert -n OLDER_COMMIT^..NEWER_COMMIT
and everything is good. I'm using git version1.7.9.6
.使用 git rebase -i 来将相关提交压缩为一个。然后你只需恢复一次提交。
Use
git rebase -i
to squash the relevant commits into one. Then you just have one commit to revert.