2

I want to know what's going on. I created an HTML file and put some lines in it

this is first line
this is second line
this is third line
this is fourth line

and committed after each line like commit a, commit b, commit c, commit d respectively.

Now I performed a revert to commit c, but it throws an error :

could not revert 82b69e5... c

hint: after resolving the conflicts, mark the corrected paths <br>
hint: with 'git add <paths>' or 'git rm <paths>' <br>
hint: and commit the result with 'git commit'<br>

I want to know how git-revert works. I know something like it "undoes a commit and add a new commit", but don't know to successfully use it.

Lasse V. Karlsen
  • 350,178
  • 94
  • 582
  • 779
  • 1
    I am pretty sure there are duplicates here on Stack Overflow that already answers this. – Lasse V. Karlsen Apr 10 '19 at 08:16
  • Like answered [here](https://stackoverflow.com/questions/29242382/what-are-the-mechanics-of-git-revert-command), `git revert` "undoes" the changes introduced by one commit by making another commit that removes those changes. If the original commit added a line, the revert commit will remove that line. If the original commit removed a line, the revert commit will put it back. If the original commit changes a line, the revert will try to undo those changes. – Lasse V. Karlsen Apr 10 '19 at 08:17
  • 1
    The reason why you got an error message is because of a conflict, the changes introduced by the `c` commit are too close to those introduced in other commits, and thus git safeguards by asking you to "resolve the conflict", in essence figure out what the right result should be. – Lasse V. Karlsen Apr 10 '19 at 08:17
  • @LasseVågsætherKarlsen your suggestion worked for me, probably i wasn't leaving much space. – Prashant Singh Apr 10 '19 at 08:30

2 Answers2

3

It creates an invert patch to the commit you want to revert, so in your case, the commit c looked like:

 this is first line
 this is second line
+this is third line
# End of file

Then, from d you run git revert c, so it tries to create the following and apply it on your tree:

 this is first line
 this is second line
-this is third line
# End of file

However, your file looks like:

this is first line
this is second line
this is third line
this is fourth line
# End of file

So the created patch does not apply (end of file vs fourth line conflicting). So when Git tells you:

could not revert 82b69e5... c
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add ' or 'git rm '
hint: and commit the result with 'git commit'

It means "I tried to do what you asked but I face a case I can't solve", so you need to either:

Most likely, your resolution would be:

this is first line
this is second line
this is fourth line
padawin
  • 3,508
  • 8
  • 16
  • Having a conflict is not an issue in itself. It is a perfectly normal thing to happen when two changes happen in a common place. Does this answer however you question about answering how `git revert` works? – padawin Apr 10 '19 at 10:57
  • yes, I was having difficulty in resolving the merge conflict. I corrected the conflict and then after that I used git revert --continue. It solved my problem – Prashant Singh Apr 10 '19 at 16:16
1

The git revert command can be considered an 'undo' type command, however, it is not a traditional undo operation.

Essentially, it undoes everything that was done in the specified commit and then creates a new commit in the process. You can check this for more information.

And regarding your issue, you are getting merge conflicts. For fixing those conflicts you can use the git mergetools (Ex. Meld).

shrikant1712
  • 3,918
  • 20
  • 39