gitgithubrevertgit-revert

I am confused about how git revert works


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.


Solution

  • 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