1

I'm pretty new to using revert in git, and I'm wondering if something like the following can be done. Let's say I have made a few commits already, and I have a (self-explanitory) text file which looks like the following:

This line was added at commit AAAAA
This line was added at commit BBBBB
This line was added at commit CCCCC

I would like to use revert to remove the changes made by commit BBBBB, to get a file that looks like this:

This line was added at commit AAAAA
This line was added at commit CCCCC

However, reverting commit BBBBB results in a merge conflict. Is there any way to avoid the conflict?

GJStein
  • 648
  • 5
  • 13
  • Did you try this -> http://stackoverflow.com/questions/2318777/undo-a-particular-commit-in-git ? – unrealsoul007 Jul 18 '15 at 16:54
  • 1
    @unrealsoul007 that just says to use `git revert` – melpomene Jul 18 '15 at 16:57
  • @unrealsoul007 Yes. I'm able to revert the commit, but it invariably results in conflicts. I can resolve the conflicts by hand or with a tool, but it's rather inconvenient (especially for large files). If this can be done automatically, it would be of great use to me. – GJStein Jul 18 '15 at 16:57
  • 1
    Merge conflicts are pretty much unavoidable when they happen. There's really no magical way for git to just know what you want – Retsam Jul 18 '15 at 16:59
  • @Retsam I have little familiarity with precisely how the commits are stored within git, so I wasn't sure. If this is true, then I'll bite the bullet and deal with it. Thanks! – GJStein Jul 18 '15 at 17:03
  • @GJStein AFAIK git stores the state of all files for each commit, but it can dynamically compute diffs between two commits (= states of the repo) to generate patches. – melpomene Jul 18 '15 at 17:06

2 Answers2

1

However, reverting commit BBBBB results in a merge conflict. Is there any way to avoid the conflict?

To find out exactly what's causing the conflict, do

git checkout --merge --conflict=diff3 -- path/to/file

which will show you the original and both changed versions of the text git's unwilling to automerge.

What boils down to is, git is merging the change from the reverted commit to its parent with the change from the reverted commit to the current version:

 bbbbb verson -> aaaaa version,

and

 bbbbb version -> cccccc version (+ whatever else)

and the change hunks overlap, so, git's not going to automerge.

As a practical matter, given the amount of merging being done in several huge projects, you can be sure any automerging git refuses to do produces bad results too often to tolerate.

jthill
  • 55,082
  • 5
  • 77
  • 137
0

if you want to revert changes only on single file, assuming, that you want to revert part of the last commit, you can just checkout the version of file from previous commit

git checkout HEAD~1 directory/path/file.txt
git add directory/path/file.txt
git commit -m "directory/path/file.txt reverted"

Basically git checkout HEAD . reset all files from working directory or only specified directories or only single files, to specified state of given commit.

Also if you want to revert one directory to state of 3 commits ago, you can:

git checkout HEAD~3 some/directory/
noisy
  • 6,495
  • 10
  • 50
  • 92