英文:
Git is showing (modify/delete) conflict while using the revert command
问题
I made an empty repository and created a file and committed it and then I made a change and committed it again.
Now I tried to revert to the first commit by using git revert command but it is showing an error saying
git revert 022381f9ba075b7c3549e18340837119c833c12c
CONFLICT (modify/delete): file1.txt deleted in (empty tree) and modified in HEAD. Version HEAD of file1.txt left in tree.
error: could not revert 022381f... Added file1.txt and added text to it
I am expecting to revert it to the first commit (022381f9ba075b7c3549e18340837119c833c12c) but it is showing an error
英文:
I made an empty repository and created a file and committed it and then I made a change and committed it again.
Now I tried to revert to the first commit by using git revert command but it is showing an error saying
git revert 022381f9ba075b7c3549e18340837119c833c12c
CONFLICT (modify/delete): file1.txt deleted in (empty tree) and modified in HEAD. Version HEAD of file1.txt left in tree.
error: could not revert 022381f... Added file1.txt and added text to it
I am expecting to revert it to the first commit (022381f9ba075b7c3549e18340837119c833c12c) but it is showing an error
vanum@DESKTOP-BF2U9VF MINGW64 ~/OneDrive/Desktop/project (master)
$ git log
commit e6cc90a1c488288ac5d6fb626257057f77d50b39 (HEAD -> master)
Author: vanumdaniel7 <vanumdaniel7@gmail.com>
Date: Mon Mar 20 22:22:43 2023 +0530
Made a change to the file file1.txt
commit 022381f9ba075b7c3549e18340837119c833c12c
Author: vanumdaniel7 <vanumdaniel7@gmail.com>
Date: Mon Mar 20 22:22:10 2023 +0530
Added file1.txt and added text to it
vanum@DESKTOP-BF2U9VF MINGW64 ~/OneDrive/Desktop/project (master)
$ git revert 022381f9ba075b7c3549e18340837119c833c12c
CONFLICT (modify/delete): file1.txt deleted in (empty tree) and modified in HEAD. Version HEAD of file1.txt left in tree.
error: could not revert 022381f... Added file1.txt and added text to it
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".
vanum@DESKTOP-BF2U9VF MINGW64 ~/OneDrive/Desktop/project (master|REVERTING)
$
答案1
得分: 1
git revert
命令存在关于要指定的提交的误解:
git revert
不会回滚到给定提交之前的所有更改。
相反,git revert
只会通过创建具有相反更改的新提交来撤销给定提交的更改。
所以你要做的是撤销第二个提交,将代码有效地更改回之前的状态。
git revert e6cc90a1c488288ac5d6fb626257057f77d50b39
之后,你的历史记录中将有三个提交:初始提交,第二个提交和一个撤销第二个提交更改的第三个提交。
或者
如果你想将本地分支回滚一个提交,可以使用git reset HEAD~
。
请注意,如果你已经推送了第二个提交,这不是一个好主意,因为下次你拉取时会再次出现(除非你强制推送,但这是另一个话题)。如果已经推送了,通常使用git revert
是更好的选择。
额外信息:你可以使用git revert --abort
来中止当前的撤销操作。
(我刚刚意识到git在你的截图中提到了这一点,但我还是会在这里留下这个备注,以防万一)
英文:
There seems to be a misunderstanding about what commit to specify with the git revert
command:
git revert
does not revert everything up to the given commit.
git revert
instead only reverts the changes of the given commit, by creating a new commit with the opposite changes.
So what you want to do it revert the second commit, to effectively change the code back to what it was before.
git revert e6cc90a1c488288ac5d6fb626257057f77d50b39
After that you'll have three commits in your history: The initial commit, the second commit and a third commit that reverts the changes of the second commit.
OR
If you want to move the local branch back by one commit you can use git reset HEAD~
.
Note, that is not a good idea if you've already pushed your second commit,<br>
because it will return next time you pull. (Unless you force-push, but that's a topic on its own).<br>
If you pushed already, then git revert
is usually the better option.
Side-Note: You can abort your current revert with git revert --abort
<br>
(I just realized git hinted at that in your screenshot, but I'll leave this note here anyway; just in case)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论