Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent in git of "hg cat" or "svn cat"

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?

like image 662
Richard Boulton Avatar asked Jan 15 '10 12:01

Richard Boulton


People also ask

What is cat in git?

The cat (short for “concatenate“) command is one of the most frequently used commands in Linux/Unix-like operating systems. cat command allows us to create single or multiple files, view content of a file, concatenate files and redirect output in terminal or files.

How does git cat file work?

Git's cat-file doesn't really stand for "concatenate"; it simply is a reference to the behavior of the cat command. If --batch or --batch-check is given, cat-file will read objects from stdin, one per line, and print information about them.


7 Answers

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.

like image 194
Tor Valamo Avatar answered Oct 16 '22 01:10

Tor Valamo


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.

like image 22
Scott Chacon Avatar answered Oct 16 '22 00:10

Scott Chacon


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.
like image 21
Andrew Aylett Avatar answered Oct 16 '22 00:10

Andrew Aylett


Also work with branch names (like HEAD in the 1st p) :

git show $branch:$filename
like image 25
RzR Avatar answered Oct 16 '22 00:10

RzR


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

like image 40
John Feminella Avatar answered Oct 16 '22 01:10

John Feminella


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

like image 38
RyanWilcox Avatar answered Oct 15 '22 23:10

RyanWilcox


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)

like image 36
Brian Agnew Avatar answered Oct 15 '22 23:10

Brian Agnew