我对git revert的工作原理感到困惑

我对git revert的工作原理感到困惑,git,github,revert,git-revert,Git,Github,Revert,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 correc

我想知道发生了什么事。我创建了一个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>
无法还原82b69e5。。。C
提示:解决冲突后,标记更正的路径
提示:使用“git add”或“git rm”
提示:并使用“git commit”提交结果

我想知道git 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'
这意味着“我试着按你的要求去做,但我面临一个我无法解决的案件”,因此你需要:

  • 解决冲突,使用
  • 或者使用git revert--quit取消还原
最有可能的是,您的解决方案是:

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

git revert命令可以被视为一个“undo”类型的命令,但是,它不是一个传统的undo操作

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


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

我非常确定堆栈溢出中存在重复项,这些重复项已经解决了这个问题。与Answeed一样,
git revert
“撤消”一次提交引入的更改,通过进行另一次提交来删除这些更改。如果原始提交添加了一行,则恢复提交将删除该行。如果原始提交删除了一行,则恢复提交会将其放回。如果原始提交更改了一行,则还原将尝试撤消这些更改。您收到错误消息的原因是冲突,
c
commit引入的更改与其他提交中引入的更改太接近,因此git会通过要求您“解决冲突”来保护您,从本质上说,你应该弄清楚正确的结果应该是什么。@LasseVågsætherKarlsen你的建议对我很有效,可能我没有留下太多空间。冲突本身不是问题。当两个变化发生在同一个地方时,这是完全正常的事情。这是否回答了您关于回答git revert如何工作的问题?是的,我在解决合并冲突时遇到了困难。我纠正了冲突,然后使用git revert--continue。它解决了我的问题