1

I'm learnig GIT. I created a .txt file where I added three entries in the following order:

ABC
123
DEF

Each entry has been commited. Now, I would like to revert a commit where I added "123" to get the following effect:

ABC
DEF

or

ABC

DEF

But when I'm trying to use git revert <123 commit hash> I get an error. Does anybody know how to solve this problem?

MINGW64 ~/Desktop/GIT revert test (main)
$ cat file_1.txt
ABC
123
DEF

MINGW64 ~/Desktop/GIT revert test (main)
$ git log --oneline
6c358c2 (HEAD -> main) DEF added
e2dffa0 123 added
46abdb3 ABC added
39d0250 empty .txt file created

MINGW64 ~/Desktop/GIT revert test (main)
$ git revert e2dffa0
Auto-merging file_1.txt
CONFLICT (content): Merge conflict in file_1.txt
error: could not revert e2dffa0... 123 added
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git revert --continue".
hint: You can instead skip this commit with "git revert --skip".
hint: To abort and get back to the state before "git revert",
hint: run "git revert --abort".

MINGW64 ~/Desktop/GIT revert test (main|REVERTING)
$

Also, when I modified my file1.txt for the first time I got that message (maybe this will help):

warning: LF will be replaced by CRLF in file_1.txt. The file will have its original line endings in your working directory

askren
  • 15
  • 4
  • 2
    Does this answer your question? [How to resolve merge conflicts in a Git repository?](https://stackoverflow.com/questions/161813/how-to-resolve-merge-conflicts-in-a-git-repository) – 0x5453 Jan 27 '22 at 19:59
  • 4
    When 123 was added, DEF didn't exist. Git is telling you it doesn't know if it should keep `DEF` or not as the result of reverting e2dffa0. (ABC was the *last* line before e2dffa0 was created; should it still be the last line?) – chepner Jan 27 '22 at 20:03
  • Ok, but why [here](https://youtu.be/mRm22eIxHKg?t=30) and [here](https://youtu.be/UmE0uf5UMzA?t=159) everything works fine? – askren Jan 28 '22 at 13:07
  • @askren In the first example, there is whitespace separating the reverted lines from the lines that changed in the following commit, so Git can resolve the revert cleanly. In the second example, the most recent commit is in a different file, so there is no possibility for a conflict during the revert. – 0x5453 Jan 28 '22 at 13:58
  • @0x5453 Whitespace? You mean [this](https://imgur.com/a/CbCWLYe)? I tried it and I still can't reproduce the revert there from the first video. – askren Jan 30 '22 at 11:49

2 Answers2

1

As the error states, the revert caused a merge conflict.

You need to resolve the conflict and then use git revert --continue to finalize the revert.

0x5453
  • 12,753
  • 1
  • 32
  • 61
1

Git's merge algorithm considers two adjacent lines which have both changed but in different commits to be a merge conflict. This is a prudent approach, since in many source files, this scenario leads to broken (syntactically invalid or logically incorrect) code.

When you reverted the middle commit, it caused a conflict because it was modified on both sides by its bordering commits. You'd need to resolve those conflicts and once you've done so, run git add to mark it as resolved. I believe the next step in this case is to run git revert --continue, but if you run git status, Git will tell you what to do for certain.

bk2204
  • 64,793
  • 6
  • 84
  • 100