Search code examples
gitgit-revertgit-merge-conflict

Why does git revert give me conflicts


Saying that I have an empty git repository. Here is the commands that I execute:

echo 'aaa' > file
git add .
git commit -m 'a'
echo 'bbb' >> file
git commit -am 'b'
echo 'ccc' >> file
git commit -am 'c'

Then I execute git reflog to get the log:

d3f79b4 HEAD@{0}: commit: c
4c79a7f HEAD@{1}: commit: b
a31df0d HEAD@{2}: commit (initial): a

Now I want to revert to the commit: b, meaning that I want to change the file from

aaa
bbb
ccc

to

aaa
bbb

I try to execute the command: git revert 4c79a7f but I get some error message about conflict:

error: could not revert 4c79a7f... b
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'

And the file becomes:

aaa
<<<<<<< HEAD
bbb
ccc
=======
>>>>>>> parent of 4c79a7f... b

I can't understand why I get such a conflict.

Besides, if I create three files at each commit like this:

touch file1
git add .
git commit -m 'a'
touch file2
git commit -am 'b'
touch file3
git commit -am 'c'

I can revert to any historical commit.

I'm learning the git, so I knew that I could use git reset but I want to learn git revert now.


Solution

  • When git try to do a revert, it generate for each file a patch (using the diff format of files) which is the opposite of diff done of changes introduced by the commit.

    Your "problem" is due to the diff format and how a patch is applied.

    Example of diff:

    diff --git a/GitUI/UserControls/RevisionGrid/RevisionGridToolTipProvider.cs b/GitUI/UserControls/RevisionGrid/RevisionGridToolTipProvider.cs
    index 985995f39..91af87f84 100644
    --- a/GitUI/UserControls/RevisionGrid/RevisionGridToolTipProvider.cs
    +++ b/GitUI/UserControls/RevisionGrid/RevisionGridToolTipProvider.cs
    @@ -23,7 +23,6 @@ public void OnCellMouseEnter()
                 _toolTip.AutoPopDelay = 32767;
             }
    
    -        private int _oldIndex = -1;
             public void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
             {
                 var revision = _gridView.GetRevision(e.RowIndex);
    @@ -36,9 +35,8 @@ public void OnCellMouseMove(DataGridViewCellMouseEventArgs e)
                 var oldText = _toolTip.GetToolTip(_gridView);
                 var newText = GetToolTipText();
    
    -            if (newText != oldText || _oldIndex != e.RowIndex)
    +            if (newText != oldText)
                 {
    -                _oldIndex = e.RowIndex;
                     _toolTip.SetToolTip(_gridView, newText);
                 }
    

    You could see code added (+) and deleted (-). But you also could see that there is context lines around lines modified.

    If one of the context line in the file on which the patch is applied has changed, the patch is not applied automatically and you have a merge conflict.

    Which is your case.

    You don't have conflicts on the lines modified but on context lines...