tags:

views:

394

answers:

6

I want to extract a copy of the latest version of a file held in a git repository, and pass it into a script for some processing. With svn or hg, I just use the "cat" command:

Print the specified files as they were at the given revision. If no revision is given, the parent of the working directory is used, or tip if no revision is checked out.

(that's from the description of hg cat in the hg documentation)

What's the equivalent command to do this with git?

+8  A: 
git show rev:path/to/file

Where rev is the revision.

See http://git.or.cz/course/svn.html for a comparison of git and svn commands.

Tor Valamo
This works for me, yay! (Using HEAD as the revision) I think I need to use the path relative to the top of the repository, which is a bit of a pain, but workable.
Richard Boulton
Was checking that it was all working as I wanted, first. :)
Richard Boulton
+1  A: 

There doesn't appear to be a direct substitute. This blog entry details how to do the equivalent by determining the latest commit, then determining the hash for the file in that commit, and then dumping it out.

git log ...
git ls-tree ...
git show -p ...

(the blog entry has typos and uses the above with the command svn)

Brian Agnew
Blog entry was helpful, despite the typos.
Richard Boulton
+1  A: 

Use git show, as in git show commit_sha_id:path/to/some/file.cs.

John Feminella
+1  A: 

git show is the command you are looking for. From the documentation:

   git show next~10:Documentation/README
          Shows the contents of the file Documentation/README as they were
          current in the 10th last commit of the branch next.
Andrew Aylett
That doesn't seem to work: running "git show next~1:README" producesfatal: ambiguous argument 'next~1:README': unknown revision or path not in the working tree.Use '--' to separate paths from revisions
Richard Boulton
Do you have a branch called 'next'? If you want the current branch, use 'HEAD' instead.
Andrew Aylett
+1  A: 

I wrote a git cat shell script, which is up on github

RyanWilcox
Looking at the code for that was helpful, though I wanted a standalone python script, and didn't need all the features of git cat. Thanks.
Richard Boulton
+3  A: 

there is "git cat-file" which you can run like this:

$ git cat-file blob v1.0:path/to/file

where you can replace 'v1.0' with the branch, tag or commit SHA you want and then 'path/to/file' with the relative path in repository. You can also pass '-s' to see the size of the content if you want.

might be closer to the 'cat' commands you are used to, though the previously mentioned 'show' will do much the same thing.

Scott Chacon