views:

5466

answers:

3

I have a Git repository and I'd like to seem how some file looked a few months ago. I found the revision at that date, it's: 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8. I need to see what did one file look like and also save that to file.

I managed to see the file using gitk, but it does not have an option to save it. I tried with command line tools, the closest I got was:

git-show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8 my_file.txt

However, this command shows a diff and not the file contents. I know I can later use something like PAGER=cat and redirect output to a file, but I don't know how to get to the actual file content.

Basically, I'm looking for something like svn cat.

+6  A: 

Nevermind, I just found it. You need to provide full path to the file:

git-show 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8:full/repo/path/to/my_file.txt
Milan Babuškov
+1  A: 

May be git checkout 27cf8e84bb88e24ae4b4b3df2b77aab91a3735d8 ? If you want to keep your repository just clone it and make git checkout on copy of the repository.

+10  A: 

To complete your own answer, the syntax is indeed

git show object
git show $REV:$FILE
git show HEAD^^^:test/test.py

The command takes the usual style of revision, meaning you can use any of the following:

  1. HEAD + x number of ^ characters
  2. The SHA1 hash of a given revision
  3. The first few (maybe 5) characters of a given SHA1 hash

Tip It's important to remember that when using "git show", always specify a path from the root of the repository, not your current directory position.


Before git1.5.x, that was done with some plumbing:

git ls-tree <rev>
show a list of one or more 'blob' objects within a commit

git cat-file blob <file-SHA1>
cat a file as it has been committed within a specific revision (similar to svn cat). use git ls-tree to retrieve the value of a given file-sha1

git cat-file -p $(git-ls-tree $REV $file | cut -d " " -f 3 | cut -f 1)::

git-ls-tree lists the object ID for $file in revision $REV, this is cut out of the output and used as an argument to git-cat-file, which should really be called git-cat-object, and simply dumps that object to stdout.

VonC