【发布时间】:2019-02-24 10:47:25
【问题描述】:
说我有一个空的 git 存储库。这是我执行的命令:
echo 'aaa' > file
git add .
git commit -m 'a'
echo 'bbb' >> file
git commit -am 'b'
echo 'ccc' >> file
git commit -am 'c'
然后我执行git reflog获取日志:
d3f79b4 HEAD@{0}: commit: c
4c79a7f HEAD@{1}: commit: b
a31df0d HEAD@{2}: commit (initial): a
现在我想恢复为commit: b,这意味着我想将file从
aaa
bbb
ccc
到
aaa
bbb
我尝试执行命令:git revert 4c79a7f,但我收到一些关于冲突的错误消息:
error: could not revert 4c79a7f... b
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'
file 变成:
aaa
<<<<<<< HEAD
bbb
ccc
=======
>>>>>>> parent of 4c79a7f... b
我不明白为什么会发生这种冲突。
此外,如果我在每次提交时创建三个文件,如下所示:
touch file1
git add .
git commit -m 'a'
touch file2
git commit -am 'b'
touch file3
git commit -am 'c'
我可以恢复到任何历史提交。
我正在学习 git,所以我知道我可以使用 git reset,但我现在想学习 git revert。
【问题讨论】:
-
git revert将尝试通过在您的历史记录中的当前点应用其反向作为新提交来删除相关提交引入的更改。如果提交中引入的更改及其负面影响将应用于随后已删除或更改的文件,则 git 将无法执行此操作,因为它不了解如何执行此操作。例如,如果您将代码行中的一个简单数字从 1 增加到 2:variable += 2;(was 1) 在提交中,然后删除整行,如果您现在尝试恢复此更改,git 将不知道是什么去做。
标签: git git-revert git-merge-conflict