0

Currently, when I have to do it, here are the steps that I follow:

  • Revert the commit (without creating a new commit, of course)
  • Reset all the modification introduced by the “revert,” except the one of the file that I wand to revert
  • Stage the reversal of the file which I want to revert and amend the last commit

Is there a more straightforward way to do that?

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Samuel Rossille
  • 18,940
  • 18
  • 62
  • 90
  • possible duplicate of [Undo last Git commit](http://stackoverflow.com/questions/927358/undo-last-git-commit) – Brian Roach Dec 21 '12 at 15:55
  • @BrianRoach this is not the same! I don't want to revert my commit. I want to amend it to reset a single file and i'm looking for a more straightforward way to do it than what i'm doing today – Samuel Rossille Dec 21 '12 at 15:57

1 Answers1

3

Checkout the previous state of the file and amend the commit.

git checkout HEAD^ -- file.txt
git commit --amend

If you don't need to amend the commit log message the last command can be

git commit --amend -C HEAD

which doesn't even open your editor to edit the commit log, it just amends it.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
  • Tx, but my question is on the "Alter the file to represent the state you want to be" part. I'm looking for a more straightforward way to do it that what I'm doing today (which is explained in the question) – Samuel Rossille Dec 21 '12 at 15:59
  • Didn't know about the possibility to checkout a single file, thanks. – Samuel Rossille Dec 21 '12 at 16:01