Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I'm not able to revert a file while it's conflicting. git

Tags:

git

revert

Here is what I've done:

  • Create folder testA
  • Create a file "a.txt" with it's contents "a"
  • Git init into the folder testA
  • git add .
  • git commit -a -m "first"
  • change the file's "a.txt" contents to "b"
  • git add .
  • git commit -a -m "changed to b"
  • git log (to get the commit id of the "first" )
  • git revert a90deafe08c9f8f0d7b94d3d74d40be2bd65b161

I get in console:

error: could not revert a90deaf... first
hint: after resolving the conflicts, mark the
hint: with 'git add <paths>' or 'git rm <paths
hint: and commit the result with 'git commit'

Well, I would like to have all the files at the state of the previous commit, what should I do know?

UPDATE What I would want:

  • I would like to keep the "changed to b" commit and have a third one that is exactly the same as the "first" commit. I never ever want to lose any file.
  • To work with binary files (see next point)
  • I don't care how binary files are manage as long as I can have them in the state of the commit, so merge is not necessary in this case. I just want something easier: replace file A from commit X to commit Y, no complications with merge needed.
like image 722
Totty.js Avatar asked Oct 22 '12 09:10

Totty.js


1 Answers

First, get back to a good state with:

git revert --abort

Next, look at what you're asking. git revert takes the commit id of the commit you want to undo. The first commit sets a.txt to be:

a

The second commit makes it have:

b

And you want to revert the first commit. So what that is saying is that you'd like to remove the addition of the a line, and the addition of the file which is a conflict. The first commit added the file, and the second commit modified it. So git doesn't know what to do now that you're asking to remove it.

I assume by "all the files at the state of the previous commit" that you want to get back to the state where a.txt has a in it.

There are a couple of ways to get back to the previous state. If the commits have not been shared, you can simply use git reset:

git reset --hard HEAD~1

This says "reset the state of my working copy and index to the previous commit". At this point, a.txt will have a in it.

If you had shared the second commit, then you don't want to do the above, because you'll be rewriting history, which causes a whole host of other issues. Instead, revert the commit, like you were trying to do above, but use the right commit. In this case, it's the HEAD commit you want to revert:

git revert HEAD

There are times when reverting a commit may conflict. In that case, it's very much like merge conflicts. You need to work through them, fixing up the conflicts and either using git add to stage the file with the modifications, or using git rm to remove the file. Once you're done, git revert --continue will let the process finish.

Update

The easiest way to go to restore the previous state, without having to do any fancy merging, is to use git checkout. You can do:

git checkout HEAD~1 -- a.txt
git commit -m "Reverted to the previous version."

No merging, no fuss. :-) HEAD~1 can be any commit, of course. I just used this based on the example you had.

like image 85
John Szakmeister Avatar answered Sep 21 '22 01:09

John Szakmeister