2

I want to revert a commit in which I have modifed files and added files:

mkdir repo_1
cd    repo_1
git   init

echo Version one>      a.txt
echo Version one>      b.txt

git add    a.txt b.txt      
git commit a.txt b.txt             -m "V.1"

echo Version two>>     a.txt
echo Version two>>     b.txt
echo Version two>>     c.txt

git add    c.txt

git commit a.txt b.txt c.txt       -m "V.2"

echo Version three>>   a.txt
echo Version three>>   b.txt
echo Version three>>   c.txt


git commit a.txt b.txt c.txt       -m "V.3"

After these steps, git log . prints

commit fdd97fc23535107202732888b240bbe2e4e08554
Author: Some One <some.one@somewhe.re>
Date:   Tue Apr 15 21:40:04 2014 +0200

    V.3

commit 8cab1785c5d653b4965045a1f77cf909c6ed4ddc
Author: Some One <some.one@somewhe.re>
Date:   Tue Apr 15 21:40:04 2014 +0200

    V.2

commit 2eb9d1561a696856beaa49c3d839863ed279a8e5
Author: Some One <some.one@somewhe.re>
Date:   Tue Apr 15 21:40:04 2014 +0200

    V.1

Now, I want to unde the changes that I commited with V.2, that is with SHA 8cab1785c5d653b4965045a1f77cf909c6ed4ddc

When I try git revert 8cab1785c5d653b4965045a1f77cf909c6ed4ddc I get a

error: could not revert 8cab178... V.2
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'

When I try git revert -n 2eb9d1561a696856beaa49c3d839863ed279a8e5 8cab1785c5d653b4965045a1f77cf909c6ed4ddc I get a

error: could not revert 2eb9d15... V.1
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'

So, how can I revert my second commit?

René Nyffenegger
  • 39,402
  • 33
  • 158
  • 293
  • Your first revert attempt didn't fail. You just encountered a conflict - resolve it and continue. Also, in your example you don't seem to be committing a.txt and b.txt at all because you haven't added/staged them. – Assaf Lavie Apr 15 '14 at 19:52
  • Is there any other message before `error: could not revert...`? – Rohit Jain Apr 15 '14 at 19:53
  • 1
    You may find your answer here. http://stackoverflow.com/questions/4114095/revert-to-previous-git-commit – Corey Berigan Apr 15 '14 at 19:53
  • 2
    You're doing it correctly: git just needs your help to apply the reverse-diff. In V.2 each file only has the "Version one" line, and ends after that. When trying to remove V.2 from the *current* files, though, there's more stuff after what, according to the reverse-patch, should be the end of file. It doesn't know what to do with that. This is the same as any other merge conflict: you must complete the merge and commit the result. – torek Apr 15 '14 at 19:53
  • @RohitJain: No, there is no other error message. – René Nyffenegger Apr 15 '14 at 19:56
  • read the fine [manpages](http://www.manpagez.com/man/1/git-revert/). – Alexej Magura Apr 15 '14 at 20:48

0 Answers0