ankaAJ
ankaAJ

Reputation: 47

Conflict during git revert on a single file

I'm using git revert on a very basic use case, in order to learn how it really works, but I'm facing the following issue. I've read a couple of posts on similar scenarios, but none has provided a clear answer to this in my opinion. I've surf the internet and even superficially read part of the Git documentation on reset/revert command, but still can't figure out what is going on here. Any help preciated.

These are the exact steps I'm executing, in the below order:

  1. Create a ~/gittest directory and create a testfile file inside it.
  2. Add the following line to testfile : commit 1
  3. Execute git add testfile; git commit -m 'commit 1'
  4. Add the following line to testfile : commit 2
  5. Execute git add testfile; git commit -m 'commit 2'
  6. Add the following line to testfile : commit 3
  7. Execute git add testfile; git commit -m 'commit 3'

At this point I execute git log --oneline and this is the output:

ba1810 (HEAD -> master) commit 3
88bc443 commit 2
802d820 commit 1

Now the testfile looks like this:

commit 1
commit 2
commit 3

What I'm trying to acomplish is to revert commit 88bc443 and therefore expecting for the line saying commit 2 to disapear, so testfile ends up looking like this:

commit 1
commit 3

Before attempting the revert operation I execute git status and indeed my working tree is clean:

On branch master
nothing to commit, working tree clean

So when I execute git revert 88bc443 I get the following error:

Auto-merging test
CONFLICT (content): Merge conflict in test
error: could not revert 88bc443... commit 2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add ' or 'git rm '
hint: and commit the result with 'git commit'

These are the git indicators on the tesfile after the error prompts:

commit 1
<<<<<<< HEAD
commit 2
commit 3
=======
>>>>>>> parent of 88bc443... commit 2

I haven't done any merge at all. The above are the sole instructions I have execute on the terminal. I have read how revert is suposed to work between branches after/before merging, but I don't see where would the conflict exist in this case, since there is no overriding of the line I'm trying to revert by any subsequent commit. I'm working on a single file, not even between branches or remote repositories.

Git version: 2.25.1
Operative System: Linux Mint 20.2

Upvotes: 1

Views: 381

Answers (1)

j6t
j6t

Reputation: 13387

Internally, git revert does perform a merge operation.

The merge algorithm needs three versions of a file:

  • A base version; that is the version after the commit that is reverted, which is

    commit 1
    commit 2
    
  • their version; that is the version before the commit that is reverted, which is

    commit 1
    
  • and our version; that is the version onto which the reverted commit is made, which is

    commit 1
    commit 2
    commit 3
    

As you can see, in the transition from base to theirs the line commit 2 was removed, but in the transition from base to ours, a line commit 3 was appended after the line commit 2.

Even though you might think that line commit 3 is independent from the line commit 2, the (authors of the) merge algorithm did not think the same. Rather, it is considered a noteworthy event when changes occur in adjacent lines, and for this reason the situation is flagged as a conflict.

Upvotes: 1

Related Questions