我对 git 恢复的工作方式感到困惑

I am confused about how git revert works

我想知道这是怎么回事。我创建了一个 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 是如何工作的。我知道类似的东西"undoes a commit and add a new commit",但不知道如何成功使用它。

它为您要还原的提交创建了一个反向补丁,因此在您的情况下,提交 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'

意思是"I tried to do what you asked but I face a case I can't solve",所以你需要:

  • 解决冲突,使用How to resolve merge conflicts in Git
  • 或使用git revert --quit
  • 取消还原

您的解决方案很可能是:

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

git恢复命令可以被认为是'撤销'类型的命令,但是,它不是传统的撤销操作。

本质上,它会撤消在指定提交中完成的所有操作,然后在此过程中创建一个新的提交。您可以查看 this 了解更多信息。

关于您的问题,您遇到了合并冲突。要解决这些冲突,您可以使用 git 合并工具(例如 Meld)。