git revert <hash> 由于合并而不允许,但没有给出 -m 选项
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24301390/
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
git revert <hash> not allowed due to a merge but no -m option was given
提问by user3544484
I am trying to revert to a certain 'hash' number in git, by using the 'revert' command.
我试图通过使用“revert”命令在 git 中恢复到某个“哈希”数字。
I am using the following command:
我正在使用以下命令:
git revert c14609d74eec3ccebafc73fa875ec58445471765
But, I am getting the following returned:
但是,我得到以下返回:
error: Commit c14609d74eec3ccebafc73fa875ec58445471765 is a merge but no -m option was given.
fatal: revert failed
错误:提交 c14609d74eec3ccebafc73fa875ec58445471765 是一个合并,但没有给出 -m 选项。
致命:恢复失败
As a new git user, please can you explain what is happening & what I have to do to resolve this.
作为一个新的 git 用户,请你解释一下发生了什么以及我必须做些什么来解决这个问题。
I want to revert back to this certain commit (c14609d74eec3ccebafc73fa875ec58445471765
) that I see when running git log
.
我想恢复到c14609d74eec3ccebafc73fa875ec58445471765
我在运行时看到的这个特定提交 ( ) git log
。
回答by manojlds
You are trying to revert a merge commit, and git doesn't know which parent to revert to. The -m
allows us to choose which parent to choose. See the merge commit and note down which parent you want to go to. The parent information can be seen in git log
, for example:
您正在尝试还原合并提交,而 git 不知道要还原到哪个父级。这-m
允许我们选择要选择的父级。查看合并提交并记下您要转到哪个父级。父信息可以在 中看到git log
,例如:
commit d02ee0f2179def10277f30c71c5d6f59ded3c595
Merge: dd3a24c 2462a52
提交 d02ee0f2179def10277f30c71c5d6f59ded3c595
合并:dd3a24c 2462a52
and run:
并运行:
git revert <hash> -m 1
where 1
indicates parent number 1.
其中1
表示父编号 1。
If you are trying to revert tothat commit, do:
如果你想恢复到那个承诺,做到:
git reset --hard <hash>
Understand the difference between git revert
and git reset
from the docs and decide which one you want. git revert
is the safer option, but doesn't really do what you want. It just reverts the changes of a (set of) commit. git reset
makes you move to a particular commit in history, and will rewrite your history.
了解文档之间的区别git revert
和git reset
文档之间的区别,然后决定您想要哪一个。git revert
是更安全的选择,但并没有真正做到你想要的。它只是还原(一组)提交的更改。git reset
使您移动到历史记录中的特定提交,并将重写您的历史记录。
回答by user3544484
I want to revert back to ...
我想变回...
Then you don't want git revert
, at least not like this. git revert
is for reverting the specific changes made in that commit. What you're looking for is to revert or undo all the changes made afterthat commit.
那你不要git revert
,至少不要这样。git revert
用于恢复在该提交中所做的特定更改。您正在寻找的是还原或撤消该提交后所做的所有更改。
git reset
is the command to use here.
git reset
是此处使用的命令。
git reset --hard c14609d74eec3ccebafc73fa875ec58445471765
completely resets your branch, index and work tree to that specific commit.
git reset --hard c14609d74eec3ccebafc73fa875ec58445471765
将您的分支、索引和工作树完全重置为该特定提交。
Note that the usual precautions apply: if anyone else has fetched later commits already, removing them from the history like this complicates matters for them. If you instead want to create a new commit, which simply restores the state to that of commit c14609d74eec3ccebafc73fa875ec58445471765, you can use git rm
and git checkout
:
请注意,通常的预防措施适用:如果其他人已经获取了以后的提交,像这样将它们从历史记录中删除会使问题复杂化。如果你想创建一个新的提交,它只是将状态恢复到提交 c14609d74eec3ccebafc73fa875ec58445471765 的状态,你可以使用git rm
和git checkout
:
git rm -r .
git checkout c14609d74eec3ccebafc73fa875ec58445471765 .
(The rm
is needed to make sure newly added files also get removed.)
This lets you then create a new commit, on top of your local history, which undoes every change since c14609d74eec3ccebafc73fa875ec58445471765.
(rm
需要确保新添加的文件也被删除。)这使您可以在本地历史记录之上创建一个新的提交,这会撤消自 c14609d74eec3ccebafc73fa875ec58445471765 以来的所有更改。