恢复 git 中的一系列提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4991594/
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
Revert a range of commits in git
提问by Alex Spurling
How can I revert a range of commits in git? From looking at the gitrevisionsdocumentation, I cannot see how to specify the range I need. For example:
如何在 git 中恢复一系列提交?通过查看gitrevisions文档,我看不到如何指定我需要的范围。例如:
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.
其中 F 包含 BD 的反向。
回答by VonC
What version of Git are you using?
你使用的是哪个版本的 Git?
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 pageis only for the currentGit version (1.7.4+).
仅在 Git1.7.2+ 中支持恢复多次提交:请参阅“使用多次恢复回滚到旧提交。”了解更多详细信息。
当前git revert
手册页仅适用于当前Git 版本 (1.7.4+)。
As the OP Alex Spurlingreports in the comments:
正如OP Alex Spurling在评论中报道的那样:
Upgrading to 1.7.4 works fine.
To answer my own question, this is the syntax I was looking for:
升级到 1.7.4 工作正常。
要回答我自己的问题,这是我正在寻找的语法:
git revert B^..D
B^
means "the first parent commit of B": that allows to include B
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?")
B^
表示“B 的第一个父提交”:允许包含B
在还原中。
请参阅“git rev-parse
指定修订部分”,其中包括<rev>^
例如HEAD^
语法:请参阅“插入符号 ( ^
) 字符的含义是什么?”)
Note that each reverted commit is committed separately.
请注意,每个还原的提交都是单独提交的。
Henrik Nclarifies in the comments:
git revert OLDER_COMMIT^..NEWER_COMMIT
As shown below, you can revert without committing right away:
如下所示,您可以在不立即提交的情况下进行恢复:
git revert -n OLDER_COMMIT^..NEWER_COMMIT
git commit -m "revert OLDER_COMMIT to NEWER_COMMIT"
回答by Ramast
If you want to revert commit range B to D (at least in git version 2) in a single commit, you can do
如果您想在一次提交中将提交范围 B 恢复为 D(至少在 git 版本 2 中),您可以这样做
git revert -n B^..D
This revert the changes done by commits from B's parent commit (excluded) to the D commit (included), but doesn't create any commit with the reverted changes. The revert only modifies the working tree and the index.
这会将提交所做的更改从 B 的父提交(排除)恢复到 D 提交(包括),但不会使用恢复的更改创建任何提交。恢复只修改工作树和索引。
Don't forgot to commit the changes after
之后不要忘记提交更改
git commit -m "revert commit range B to D"
You can also revert multiple unrelated commits in a single commit, using same method. for example to revert B and D but not C
您还可以使用相同的方法在单个提交中还原多个不相关的提交。例如恢复 B 和 D 但不恢复 C
git revert -n B D
git commit -m "Revert commits B and D"
Reference: https://www.kernel.org/pub/software/scm/git/docs/git-revert.html
参考:https: //www.kernel.org/pub/software/scm/git/docs/git-revert.html
Thanks Honza Haeringfor the correction
回答by Orlando
Doing git revert OLDER_COMMIT^..NEWER_COMMIT
didn't work for me.
做git revert OLDER_COMMIT^..NEWER_COMMIT
对我不起作用。
I used git revert -n OLDER_COMMIT^..NEWER_COMMIT
and everything is good. I'm using git version 1.7.9.6
.
我用过git revert -n OLDER_COMMIT^..NEWER_COMMIT
,一切都很好。我正在使用 git 版本1.7.9.6
。
回答by Graham Borland
Use git rebase -i
to squash the relevant commits into one. Then you just have one commit to revert.
用于git rebase -i
将相关提交压缩为一个。然后你只有一个提交来恢复。