Search code examples
gitgithubconflict

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

vanum@DESKTOP-BF2U9VF MINGW64 ~/OneDrive/Desktop/project (master)
$ git log
commit e6cc90a1c488288ac5d6fb626257057f77d50b39 (HEAD -> master)
Author: vanumdaniel7 <[email protected]>
Date:   Mon Mar 20 22:22:43 2023 +0530

    Made a change to the file file1.txt

commit 022381f9ba075b7c3549e18340837119c833c12c
Author: vanumdaniel7 <[email protected]>
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)
$ 

Solution

  • 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,
    because it will return next time you pull. (Unless you force-push, but that's a topic on its own).
    If you pushed already, then git revert is usually the better option.


    Side-Note: You can abort your current revert with git revert --abort
    (I just realized git hinted at that in your screenshot, but I'll leave this note here anyway; just in case)