git 相当于“hg cat”或“svn cat”的git

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2071288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 04:01:47  来源:igfitidea点击:

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

gitversion-control

提问by Richard Boulton

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:

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

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)

(这是来自 hg 文档中 hg cat 的描述)

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

用 git 执行此操作的等效命令是什么?

回答by Tor Valamo

git show rev:path/to/file

Where revis the revision.

其中rev是修订版。

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

有关git 和 svn 命令的比较,请参见http://git.or.cz/course/svn.html

回答by Scott Chacon

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

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

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

$ 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.

您可以在其中将“v1.0”替换为所需的分支、标记或提交 SHA,然后将“路径/到/文件”替换为存储库中的相对路径。如果需要,您还可以通过“-s”来查看内容的大小。

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

可能更接近您习惯的“cat”命令,尽管前面提到的“show”会做很多相同的事情。

回答by Andrew Aylett

git showis the command you are looking for. From the documentation:

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.

回答by RzR

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

还可以使用分支名称(如第一个 p 中的 HEAD):

git show $branch:$filename

回答by John Feminella

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

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

回答by RyanWilcox

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

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

回答by Brian Agnew

There doesn't appear to be a directsubstitute. This blog entrydetails how to do the equivalent by determining the latestcommit, 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)

(博客条目有错别字,并在命令中使用了上述内容svn

回答by neniu

None of the git showsuggestions truly satisfy because (try as I might), I can not find a way not to get the metadata cruft from the top of the output. The spirit of cat(1) is just to show the contents. This (below) takes a file name and an optional number. The number is how commits you want to go back. (Commits that changed that file. Commits that do not change the target file are not counted.)

没有任何git show建议真正令人满意,因为(尽我所能),我找不到不从输出顶部获取元数据的方法。cat(1) 的精神只是为了展示内容。这(下面)需要一个文件名和一个可选的数字。数字是您想要返回的提交方式。(更改该文件的提交。不更改目标文件的提交不计算在内。)

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

shows the contents of filename.txt as of the latest commit of filename.txt and the contents from 3 commits before that.

显示 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 = 
gcat () { if [ $# -lt 1 ]; then echo "Usage: $FUNCNAME [rev] file"; elif [ $# -lt 2 ]; then git show HEAD:./$*; else git show :./; fi }
) =~ 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 ); }

回答by Matt

For those using bash the following is a useful function:

对于那些使用 bash 的人来说,以下是一个有用的功能:

> gcat
Usage: gcat [rev] file

Put it in your .bashrcfile (you can use whatever name you like other than gcat.

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

Example usage:

用法示例:

> gcat subdirectory/file.ext

or

或者

> gcat rev subdirectory/file.ext

or

或者

##代码##