18

I changed an image file in git and it was 2 commits ago. How do I go back to 2 previous commits ago?

What is the easiest method of doing this file revert via the command line with the least amount of commands required?

Patoshi パトシ
  • 21,707
  • 5
  • 29
  • 47
  • possible duplicate of [Revert multiple git commits](http://stackoverflow.com/questions/1463340/revert-multiple-git-commits) – mechanicalfish Jan 08 '14 at 17:22

1 Answers1

31

Just check out the old version of that file:

git checkout HEAD~2 -- path/to/file

Or more explicit:

git checkout commit-id -- path/to/file
poke
  • 369,085
  • 72
  • 557
  • 602
  • what does HEAD~2 mean ? – Patoshi パトシ Jan 08 '14 at 17:32
  • 2
    `HEAD` is the currently checked out commit, and `HEAD~n` is *nth* commit before that. So it’s the version two commits ago. The `--` is used to tell Git to treat the remaining argument as a file path. – poke Jan 08 '14 at 17:38
  • wha thappens if i left out the -- : git checkout path/to/file – Patoshi パトシ Jan 08 '14 at 18:41
  • It will likely work, but in case the path is something that Git might be able to interpret as command parameters, you will get an error. So the safe route is to simply include that `--`. – poke Jan 08 '14 at 18:45