首页 > 解决方案 > 我对 git revert 的工作方式感到困惑

问题描述

我想知道发生了什么事。我创建了一个 HTML 文件并在其中放入了一些行

this is first line
this is second line
this is third line
this is fourth line

并在每行之后分别提交,如提交 a、提交 b、提交 c、提交 d。

现在我执行了还原以提交 c,但它抛出了一个错误:

could not revert 82b69e5... c

hint: after resolving the conflicts, mark the corrected paths <br>
hint: with 'git add <paths>' or 'git rm <paths>' <br>
hint: and commit the result with 'git commit'<br>

我想知道 git-revert 是如何工作的。我知道类似“撤消提交并添加新提交”之类的东西,但不知道如何成功使用它。

标签: gitgithubrevertgit-revert

解决方案


它为您要还原的提交创建一个反转补丁,因此在您的情况下,提交c看起来像:

 this is first line
 this is second line
+this is third line
# End of file

然后,从d您运行git revert c,它会尝试创建以下内容并将其应用到您的树上:

 this is first line
 this is second line
-this is third line
# End of file

但是,您的文件如下所示:

this is first line
this is second line
this is third line
this is fourth line
# End of file

所以创建的补丁不适用(文件结尾与第四行冲突)。所以当 Git 告诉你:

could not revert 82b69e5... c
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add ' or 'git rm '
hint: and commit the result with 'git commit'

它的意思是“我试图按照你的要求去做,但我遇到了一个我无法解决的案子”,所以你需要:

最有可能的是,您的解决方案是:

this is first line
this is second line
this is fourth line

推荐阅读