-2

I am very new to Git and trying to follow along with Git Prodigy by Ebenezer Don to learn about it. I am at the (early) stage wherein I am learning about git revert.

I produce the text file prac.txt

This is a random text file

I then make this my first commit (git add . followed by git commit -m "First commit."), with first 6 characters of my commit code as 5b0a47. Now I modify the file to read (I do this in vim)

This is a random text file

New version

I now proceed again with git add . followed by git commit -m "Second commit." Now I want to revert and so I use git revert 5b0a47 (I use the full code), but I get the following error:

CONFLICT (modify/delete): prac.txt deleted in (empty tree) and modified in HEAD.  Version HEAD of prac.txt left in tree.
error: could not revert 5b0a47f... First commit.
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".

I hardly know anything about merge conflicts at this point but why is this appearing? The book doesn't suggest it should appear, nor do I expect it to based on what I know about Git at this point. Nothing should be in conflict?

2
  • 5b0a47 created the file. reverting it would mean deleting it, but your later commit added text, so what should it do?
    – tkausl
    Jan 26 at 0:06
  • 1
    see Where does my git question go?
    – gnat
    Jan 26 at 5:15

1 Answer 1

6

The aim of git revert is to reverse the change caused by a given commit. But sometimes things that happen in between change the situation so that that commit is not directly reversible - at least not unless you either reverse those other things first, or tell git how to deal with the conflict.

The change made by the original commit was to create a file where there was none before, consisting only of the words line "This is a random text file".

The reverse of that is deleting a file consisting only of the words line "This is a random text file", so that is what git revert wants to do. But you don't have a file like that. You have a different file. There are at least two valid options - you could delete the entire file you have now, or you could just delete those words, and leave the rest of the file in place.

Git doesn't know what you want, so it asks you to "resolve the conflict", i.e. make the decision.

7
  • Perhaps (definitely!) I am misunderstanding the revert operation then. I had thought that the reversion I described above would be to delete the “New Version” line? Where is file deletion coming into play?
    – EE18
    Jan 26 at 0:29
  • 5b0a47 added the file. When you say git revert 5b0a47 you're asking git to undo that, which means deleting the file.
    – bdsl
    Jan 26 at 0:31
  • May be you think 'revert x' means "revert to x" - but it doesn't, it means undo the change that x caused.
    – bdsl
    Jan 26 at 0:33
  • 1
    Yes, just confirmed and it writes "To revert to the initial commit, find the hash of the commit you want to revert to (which would be the hash of the “Add README file” commit in this case), and use the git revert command followed by the commit hash. This will create a new commit that undoes the changes." That is concerning, I guess.
    – EE18
    Jan 26 at 0:47
  • 3
    At any rate, thank you so much for your help — you’ve clarified to me that I did not understand git revert at all!
    – EE18
    Jan 26 at 0:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.