在这个特定场景中使用git revert

时间:2014-12-12 05:32:15

标签: git github version-control versioning dvcs

这就是我所拥有的:

$ ls
file

$ cat file 
change 1
change 2
change 3

$ git log --oneline
8979b76 Add change 3 to file
ff1aead Add change 2 to file
53559fe Add change 1 to file

Q1:这就是我所做的和我得到的:

$ git revert 53559fe
error: could not revert 53559fe... Add change 1 to file
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
$ cat file 
change 1
change 2
change 3

为什么会这样?

Q2:然后我做了:

$ git revert --abort
$ git revert ff1aead
error: could not revert ff1aead... Add change 2 to file
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'
$ cat file 
change 1
<<<<<<< HEAD
change 2
change 3
=======
>>>>>>> parent of ff1aead... Add change 2 to file

为什么我收到错误?

如何解释文件中的标记?

1 个答案:

答案 0 :(得分:0)

有很多关于标记的信息。如果您只想合并,请查看Fix merge conflicts in Git?


我假设&#39;更改1&#39;创建了有问题的文件。如果是这种情况,第一次恢复失败,因为在更改之前文件不存在但现在确实存在。你想要的是介于两者之间:你可能想要一个文件

change 2
change 3

这不能自动计算。您没有任何标记的原因是此冲突不适用于文件的内容而是树。 git mergetool使这一点非常明确;它会询问您是要保留文件还是删除它。在这种情况下,你必须保留它并删除第一行。

我猜第二次冲突是因为没有足够的上下文可靠地自动合并。如果您打开3向合并工具,您会看到如下内容:

   LOCAL |   RESULT |   REMOTE
change 1 |          | change 1
change 2 |          |
change 3 |          |

你必须根据LOCAL和REMOTE中的信息在中间构建结果,这些信息分别引用你当前状态和变更2之前的状态。

当然,你想要的是

change 1
change 3

但从这里提供的有限背景来看,这一点并不明显,是吗?