Git根据blob哈希值找对应的commit

在对Git仓库进行瘦身的时候,根据文件大小进行排序,找到比较大的前5个文件

$ git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -5 | awk '{print$1}')"
990111a0e169578b7e1cbcd99c07d11766c7ad47 aaa.sdk
ded25f77ef2b979c804453739ea20feecef175df abc.mp4
f8b6a72df3ed2825c3a25fb1deb8b2bc37ae4d1d libiPhone-lib.a
6641cbf1992ca9fa8f60ffcce746552c15c38815 bab.mp3
04df604b314823471391b6fa984f09b038487b7a voicelib.a

但这些文件对应的哈希值都是blob,我们如果想切换到对应commit上去查看该文件的具体情况,需要知道该blob对应的commit的哈希值。可以通过以下脚本去查找:

新建一个后缀名为.pl的脚本文件git-find-blob.pl

$ vim git-find-blob.pl

然后把以下脚本放到该脚本中(源地址)

#!/usr/bin/perl
use 5.008;
use strict;
use Memoize;

# by Aristotle Pagaltzis 
# taken from thread http://stackoverflow.com/questions/223678/git-which-commit-has-this-blob
# on 6 june 2010

my $usage =
"usage: git-find-blob  []
pass the blob SHA1 as the first parameter
and then any number of arguments to git log
";
die $usage unless @ARGV;

my $obj_name = shift;

sub check_tree {
    my ( $tree ) = @_;
    my @subtree;

    {
        open my $ls_tree, '-|', git => 'ls-tree' => $tree
            or die "Couldn't open pipe to git-ls-tree: $!\n";

        while ( <$ls_tree> ) {
            /\A[0-7]{6} (\S+) (\S+)/
                or die "unexpected git-ls-tree output";
            return 1 if $2 eq $obj_name;
            push @subtree, $2 if $1 eq 'tree';
        }
    }

    check_tree( $_ ) && return 1 for @subtree;

    return;
}

memoize 'check_tree';

open my $log, '-|', git => log => @ARGV, '--pretty=format:%T %h %s'
    or die "Couldn't open pipe to git-log: $!\n";

while ( <$log> ) {
    chomp;
    my ( $tree, $commit, $subject ) = split " ", $_, 3;
    print "$commit $subject\n" if check_tree( $tree );
}

把该文件拷贝到当前Git项目的工作区,并进行授权

$ chmod +x git-find-blob.pl

然后输入要查找的blob哈希值作为参数查找commit

$ ./git-find-blob1.pl ded25f77ef2b979c804453739ea20feecef175df
e63e4bc modify file
5751b59 add mp4 file

此时列表中的e63e4bc和5751b59就是和ded25f77ef2b979c804453739ea20feecef175df相关的commit了。

此时进行git reset --hard e63e4bc 就可以看具体的情况了。

你可能感兴趣的:(Git根据blob哈希值找对应的commit)