1

I have a simple index.txt file, and I have committed after writing each line:

one

two

three

These are my commits:

19b1a80 (HEAD -> master) commit three
0305c8a commit two
f534357 commit one

But when I'm trying git revert 0305c8a I'm getting this message:

Auto-merging index.txt
CONFLICT (content): Merge conflict in index.txt
error: could not revert 0305c8a... commit two
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'

I have recently started learning git, I have tried this many times with different codes but I'm not getting why this doesn't work.

Gaël J
  • 11,274
  • 4
  • 17
  • 32
  • 1
    [revert is a merge](https://stackoverflow.com/questions/65867521/why-does-git-know-it-can-cherry-pick-a-reverted-commit/65868082#65868082). – jthill Jul 27 '21 at 18:16

1 Answers1

1

Welcome to SO! I order for git to be able to revert that revision, the line that says second would need to be followed by EOF. Given that that is not the case anymore (because instead of EOF there is a line that says three), then git does not know how to proceed and that's why it is showing you the conflict. Try this:

First revision:

foo
bar

Second revision:

foo
second
bar

Third revision:

foo
second
bar
third

Then if you try to revert the second revision, it will work like a charm (because the line that was before it, foo, is still there... and the line following it, bar, is also still there).

Say that instead of reverting, you created a fourth revision with this content:

foo
second
blah
third

(so, now instead of bar, we have blah). If you tried reverting the second revision, it would break for exactly the same reason as if failed in your experiment.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Thanks for your answer. But there is actually a separate line between any of the two line. for example, what I had written is- (line 1): one (line 2):empty (line 3):two (line 4):empty. (line 5):three and I have committed at the empty line. Then it should had worked because the line that was before two, empty-line, is still there... and the line following it, again an empty-line, is also still there! sorry if I'm not able to make my point clear. –  Jul 26 '21 at 19:00
  • @Mani: in that case, your original question was (slightly) defective, as you did not show the actual failing input. Be sure that you provide *everything necessary* for someone else to duplicate the problem. See [ask]. – torek Jul 26 '21 at 23:12
  • @Mani If you are adding an empty line between each one of the numbers, it is irrelevant. The idea is the same. If you added `second\n\n` in the second revision, the empty line will be followed by an EOF. By the time you want to revert it, there is _not_ an EOF there anymore but a new line saying `third` and so git doesn't know what to do about it and has to ask you to take a closer look. – eftshift0 Jul 27 '21 at 18:45