我是 git 初学者,我遇到了 GIT REVERT 问题。 这是我采取的步骤:
mkdir new-repo
cd new-repo
git init
echo "first line" >> test.txt
git add .
git commit -m "first line added"
echo "second line" >> test.txt
git add .
git commit -m "second line added"
然后,为了尝试并了解 git 中的工作原理,我尝试进行 git revert,看看是否可以返回到文件 test.txt 中只有第一行的版本,所以我使用:
git revert HEAD~1
但是随后,它给出了错误:
CONFLICT (modify/delete): test.txt deleted in (empty tree) and modified in HEAD. Version HEAD of test.txt left in tree.
error: could not revert b88b72d... first line
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".
即使我 git add test.txt 并且不允许我完成恢复,我也会无限地陷入这种 REVERTING 操作状态。如果我执行 git rm test.txt,它允许我完成恢复,但在这种情况下,我最终在工作场所中没有文件 test.txt。那不是我想要的。完成恢复后,我希望 test.txt 仅包含一行。我做错了什么?在这种情况下我应该怎么做才能得到我想要的东西?我感谢任何帮助!
我尝试了 git add test.txt 但它不起作用,并且我仍然停留在恢复上。
git revert --abort
git revert HEAD
当您恢复时,您基本上是在应用反向差异,并且您需要为 Git 提供标识该差异的内容。
在 Git 中,
HEAD
指的是当前分支上的最新提交,HEAD~1
指的是第二个最新的提交。因此,通过执行 git revert HEAD~1
,您试图恢复第一个提交(first line
的提交,这是第二个最新的),而不是第二次提交(second line
的提交,这是最新的)。