TIMEX
TIMEX

Reputation: 272374

For a file, how do I go back to a previous version in Git?

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.

Upvotes: 1

Views: 966

Answers (2)

Chris Johnsen
Chris Johnsen

Reputation: 225007

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

Upvotes: 1

manojlds
manojlds

Reputation: 301567

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

git checkout revision -- filename

Upvotes: 0

Related Questions