只需从提交中还原更改即可

时间:2015-12-02 15:34:12

标签: git git-revert

假设我有一个文件已经添加到一段时间并且每次都被提交到Git中。如何简单地恢复早期提交之一但保留以下所有提交?

例如,添加并提交空白file.txt。每个后续提交都会增加数量。我希望简单地撤消添加的第一个数字(1)但保留其他数字(2& 3)。似乎我应该使用revert,但我有点失落。

➜ ls
file.txt

➜ cat file.txt
1
2
3

➜ git log --oneline
afc6136 Add 3
d0cbec0 Add 2
1d6a4ff Add 1
b6c3a92 Init file.txt

➜ git revert 1d6a4ff
error: could not revert 1d6a4ff... Add 1
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.txt
<<<<<<< HEAD
1
2
3
=======
>>>>>>> parent of 1d6a4ff... Add 1

1 个答案:

答案 0 :(得分:0)

好消息:你已走上正轨!

但是,由于您所有的更改都非常接近,因此会产生冲突。从本质上说,git说'#34;自从我们回复之后,这里已经发生了太大的变化,而且我感觉不舒服 - 请整理一下并告诉我你什么时候做的事情。重做&#34;。

每个冲突的块都标记为:

<<<<<<< some commit
(chunk's content in some commit)
=======
(chunk's content in some other commit)
>>>>>>> some other commit

你应该编辑块看起来像你想要它,删除分隔符(&#34;&lt;&lt;&lt;&#34;,&#34; ===&#34;和&# 34;&gt;&gt;&gt;&#34;行),然后git add文件和git commit它(正如消息git所建议的那样)。

在你的情况下,整个文件都在块内 - 并非总是如此。在初始提交中尝试使用更大的基本文件(例如&#34;更多行&#34;),您将看到我的意思。

相关问题