由于合并而不允许返回,但不提供-m选项

时间:2021-07-14 20:35:08

I am trying to revert to a certain 'hash' number in git, by using the 'revert' command.

我试图通过使用“回复”命令恢复到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.

我想回到运行git日志时看到的这个提交(c14609d74eec3ccebafc73fa73fa875ec58445471765)。

2 个解决方案

#1


14  

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日志中可以看到父信息,例如:

commit d02ee0f2179def10277f30c71c5d6f59ded3c595

提交d02ee0f2179def10277f30c71c5d6f59ded3c595

Merge: dd3a24c 2462a52

合并:dd3a24c 2462 a52

and run:

并运行:

git revert <hash> -m 1

where 1 indicates parent number 1.

其中1表示父数字1。

If you are trying to revert to that 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 return和git reset与文档之间的区别,并决定使用哪个文档。git revert是更安全的选项,但是实际上不做您想做的事情。它只返回(一组)提交的更改。git重置使您移动到历史中特定的提交,并将重写历史。

#2


2  

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 after that commit.

然后您不希望git恢复,至少不是这样。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——硬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:

注意,通常的注意事项是这样的:如果其他人已经提交了以后的提交,那么将它们从历史记录中删除将使它们变得更加复杂。如果您想要创建一个新的commit,只需将状态恢复到commit 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来确保新添加的文件也被删除。)然后,您可以在本地历史记录的基础上创建一个新的commit,它将取消自c14609d74eec3ccebafc73fa73fa875ec58445471765以来的所有更改。

#1


14  

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日志中可以看到父信息,例如:

commit d02ee0f2179def10277f30c71c5d6f59ded3c595

提交d02ee0f2179def10277f30c71c5d6f59ded3c595

Merge: dd3a24c 2462a52

合并:dd3a24c 2462 a52

and run:

并运行:

git revert <hash> -m 1

where 1 indicates parent number 1.

其中1表示父数字1。

If you are trying to revert to that 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 return和git reset与文档之间的区别,并决定使用哪个文档。git revert是更安全的选项,但是实际上不做您想做的事情。它只返回(一组)提交的更改。git重置使您移动到历史中特定的提交,并将重写历史。

#2


2  

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 after that commit.

然后您不希望git恢复,至少不是这样。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——硬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:

注意,通常的注意事项是这样的:如果其他人已经提交了以后的提交,那么将它们从历史记录中删除将使它们变得更加复杂。如果您想要创建一个新的commit,只需将状态恢复到commit 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来确保新添加的文件也被删除。)然后,您可以在本地历史记录的基础上创建一个新的commit,它将取消自c14609d74eec3ccebafc73fa73fa875ec58445471765以来的所有更改。