1

For my current file, I want to "go back" to version __. How do I do that in Git?

I don't want the version to be my newest head. I just want to see what code was there at the time.

TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

1

If you do not want to overwrite the version in your working tree, then use git show:

git show rev:path/to/file

The rev is usually any expression (see the Specifying Revisions section in git-rev-parse(1)) that can be resolved into a commit1.
The path/to/file should be the path of the file of interest (starting from the top level of the repository).

For example, show src/config.c from the “fix bugs” commit:

% git log --oneline
7c07566 second feature
bbdbea8 fix bugs
1cf42c8 first feature
82cfa1d initial commit
% git show bbdbea8:src/config.c

bbdbea8 could also have been the full object name (bbdbea801c2a465a8c509befa46174bad4fd9fd4 in my test repository), or the expression HEAD~ or HEAD^ (if you already knew that the commit you were interested in was the first parent of the current HEAD).

1 In the rev:path syntax, rev does not have to be a “commit-ish”, it can be any “treeish” (and the path starts from the specified tree instead of the top-level).


Git can also compare different versions of files without having to explicitly extract each file:

Show the cumulative changes made to src/config.c since bbdbea8:

git diff bbdbea8 -- src/config.c

Or, sometimes it is useful to review the history bit by bit. Show each change to src/config.c since bbdbea8:

git log -p --reverse bbdbea8.. -- src/config.c
Chris Johnsen
  • 214,407
  • 26
  • 209
  • 186
0

Try the following which just checks out the particular version of the file

git checkout revision -- filename
manojlds
  • 290,304
  • 63
  • 469
  • 417
  • When I did git log, it does not show the revision #. commit 36d5813f749d2bff5764d372ebca5f57ea21a16b Date: Mon Mar 28 22:32:44 2011 -0700 after splitting views – TIMEX Apr 20 '11 at 01:20
  • Then use git checkout 36d5813f749d2bff5764d372ebca5f57ea21a16b --filename – manojlds Apr 20 '11 at 01:22