【问题标题】:Equivalent in git of "hg cat" or "svn cat"等效于“hg cat”或“svn cat”的git
【发布时间】:2010-01-15 12:07:25
【问题描述】:

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

按给定修订版打印指定文件。如果 没有给出修订,使用工作目录的父目录, 如果没有检出任何修订,则提示。

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

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

【问题讨论】:

标签: git version-control


【解决方案1】:
git show rev:path/to/file

rev 是修订版。

请参阅http://git.or.cz/course/svn.html 了解 git 和 svn 命令的比较。

【讨论】:

  • 这对我有用,耶! (使用HEAD作为revision)我觉得需要使用相对于repository顶部的路径,有点麻烦,但是可行。
  • @Richard Boulton:如果使用制表符补全,这会不那么痛苦
  • @pimlottc 在没有 ./ 的情况下对我来说工作得很好 - 可能取决于 git 版本
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

    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.
    

    【讨论】:

    • 这似乎不起作用:运行“git show next~1:README”会产生致命:不明确的参数“next~1:README”:未知修订或路径不在工作树中。使用 '--' 将路径与修订分开
    • 你有一个叫'next'的分支吗?如果您想要当前分支,请改用“HEAD”。
    【解决方案4】:

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

    git show $branch:$filename
    

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        我写了一个git cat shell脚本,就是up on github

        【讨论】:

        • 查看代码很有帮助,虽然我想要一个独立的 python 脚本,并且不需要 git cat 的所有功能。谢谢。
        【解决方案7】:

        似乎没有直接替代品。 This blog entry 详细说明了如何通过确定 latest 提交,然后确定该提交中文件的哈希值,然后将其转储出来来执行等效操作。

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

        (博文有错别字,使用上面的命令svn)

        【讨论】:

        • 博客条目很有帮助,尽管有错别字。
        【解决方案8】:

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

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

        显示截至 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 );
        }
        

        【讨论】:

          【解决方案9】:

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

          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
          

          【讨论】:

            猜你喜欢
            • 2021-08-14
            • 2012-10-18
            • 2021-09-19
            • 2015-07-15
            • 2011-06-08
            • 1970-01-01
            • 2020-02-28
            • 2022-01-15
            • 2011-01-29
            相关资源
            最近更新 更多