相当于“hg猫”或“svn猫”

我想提取git仓库中保存的最新版本的文件的副本,并将其传递到脚本中进行一些处理。 用svn或者hg,我只用“cat”命令:

在给定的修订版本中打印指定的文件。 如果没有给出修订版本,则使用工作目录的父目录,或者如果没有检出修订版本,则提示。

(这是从hg文档中对hg cat的描述)

与git做同样的命令是什么?

git show rev:path/to/file 

哪里修改。

参见http://git.or.cz/course/svn.html来比较git和svn命令。;

有可以像这样运行的“git cat-file”:

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

在那里你可以用分支replace'v1.0',标记或提交你想要的SHA然后'path / to / file'和存储库中的相对path。 如果需要,也可以传递“-s”来查看内容的大小。

可能会更接近你习惯的'猫'命令,虽然前面提到的'show'会做同样的事情。

git show是你正在寻找的命令。 从文档:

  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. 

也可以使用分支名称(如第1页的HEAD):

 git show $branch:$filename 

使用git show ,如git show commit_sha_id:path/to/some/file.cs

我写了一个git猫shell脚本,它在github上

似乎没有直接的替代品。 这个博客条目详细说明了如何通过确定最新的提交来完成相同的操作,然后确定该提交中的文件的哈希,然后将其抛出。

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

(该博客条目有错别字,并使用上面的命令svn

没有一个git showbuild议真正满足,因为(尽我所能),我找不到从输出的顶部获取元数据cruft的方法。 猫(1)的精神只是为了展示内容。 这(下面)是一个文件名和一个可选的数字。 数字是如何提交你想回去。 (提交改变了那个文件,不改变目标文件的提交不计算在内。)

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

显示filename.txt的最新提交的内容以及之前提交的3个内容。

 #!/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 ); } 

对于那些使用bash的人来说,下面是一个有用的function:

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

把它放在你的.bashrc文件中(你可以用任何你喜欢的名字而非gcat

用法示例:

 > gcat Usage: gcat [rev] file 

要么

 > gcat subdirectory/file.ext 

要么

 > gcat rev subdirectory/file.ext