Equivalent to git "hg cat" or "svn cat"

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

Print the specified files as they were in this revision. If no revision is specified, the parent element of the working directory is used, or the tip is used if the revision is not checked.

(which is from the hg cat description in the hg documentation)

What is the equivalent command for this with git?

+79
git version-control
Jan 15
source share
9 answers
git show rev:path/to/file 

Where rev is a revision.

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

+105
Jan 15
source share

there is a "git cat-file" that you can run as follows:

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

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

may be closer to the 'cat' commands you are used to, although the previously mentioned "show" will do the same.

+9
Jan 29 '10 at 6:56
source share

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. 
+5
Jan 15 '10 at 12:13
source share

Also work with branch names (e.g. HEAD in 1st p):

 git show $branch:$filename 
+3
Jan 18 '11 at 19:11
source share

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

+2
Jan 15 '10 at 12:10
source share

I wrote a git cat script shell that on github

+2
Jan 15 '10 at 12:19
source share

There seems to be no direct replacement. This blog entry details how to make an equivalent by defining the last commit, then defining a hash for the file in that commit, and then dropping it from.

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

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

+1
Jan 15 '10 at 12:10
source share

None of the git show suggestions really satisfies, because (try as I could), I cannot find a way to not get the metadata with the output top. The spirit of a cat (1) is just to show the contents. This (below) takes a file name and an optional number. A number is how you commit to go back. (Writes that he changed this file. Records that do not modify the target file are not counted.)

 gitcat.pl filename.txt gitcat.pl -3 filename.txt 

shows the contents of filename.txt with the last commit of filename.txt and contents from 3 commits before.

 #!/usr/bin/perl -w use strict; use warnings; use FileHandle; use Cwd; # Have I mentioned lately how much I despise git? (my $prog = $0) =~ s!.*/!!; my $usage = "Usage: $prog [revisions-ago] filename\n"; die( $usage ) if( ! @ARGV ); my( $revision, $fname ) = @ARGV; if( ! $fname && -f $revision ) { ( $fname, $revision ) = ( $revision, 0 ); } gitcat( $fname, $revision ); sub gitcat { my( $fname, $revision ) = @_; my $rev = $revision; my $file = FileHandle->new( "git log --format=oneline '$fname' |" ); # Get the $revisionth line from the log. my $line; for( 0..$revision ) { $line = $file->getline(); } die( "Could not get line $revision from the log for $fname.\n" ) if( ! $line ); # Get the hash from that. my $hash = substr( $line, 0, 40 ); if( ! $hash =~ m/ ^ ( [0-9a-fA-F]{40} )/x ) { die( "The commit hash does not look a hash.\n" ); } # Git needs the path from the root of the repo to the file because it can # not work out the path itself. my $path = pathhere(); if( ! $path ) { die( "Could not find the git repository.\n" ); } exec( "git cat-file blob $hash:$path/'$fname'" ); } # Get the path from the git repo to the current dir. sub pathhere { my $cwd = getcwd(); my @cwd = split( '/', $cwd ); my @path; while( ! -d "$cwd/.git" ) { my $path = pop( @cwd ); unshift( @path, $path ); if( ! @cwd ) { die( "Did not find .git in or above your pwd.\n" ); } $cwd = join( '/', @cwd ); } return join( '/', map { "'$_'"; } @path ); } 
0
Jun 06 '12 at 22:18
source share

For those using bash, a useful feature:

 gcat () { if [ $# -lt 1 ]; then echo "Usage: $FUNCNAME [rev] file"; elif [ $# -lt 2 ]; then git show HEAD:./$*; else git show $1:./$2; fi } 

Put it in your .bashrc file (you can use any name other than gcat .

Usage example:

 > gcat Usage: gcat [rev] file 

or

 > gcat subdirectory/file.ext 

or

 > gcat rev subdirectory/file.ext 
0
Jun 09 '15 at 3:45
source share



All Articles