Git根據(jù)blob哈希值找對(duì)應(yīng)的commit

在對(duì)Git倉(cāng)庫(kù)進(jìn)行瘦身的時(shí)候,根據(jù)文件大小進(jìn)行排序,找到比較大的前5個(gè)文件

$ 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

但這些文件對(duì)應(yīng)的哈希值都是blob,我們?nèi)绻肭袚Q到對(duì)應(yīng)commit上去查看該文件的具體情況,需要知道該blob對(duì)應(yīng)的commit的哈希值??梢酝ㄟ^(guò)以下腳本去查找:

新建一個(gè)后綴名為.pl的腳本文件git-find-blob.pl

$ vim git-find-blob.pl

然后把以下腳本放到該腳本中(源地址)

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

# by Aristotle Pagaltzis <http://stackoverflow.com/users/9410/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 <blob> [<git-log arguments ...>]
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 );
}

把該文件拷貝到當(dāng)前Git項(xiàng)目的工作區(qū),并進(jìn)行授權(quán)

$ chmod +x git-find-blob.pl

然后輸入要查找的blob哈希值作為參數(shù)查找commit

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

此時(shí)列表中的e63e4bc和5751b59就是和ded25f77ef2b979c804453739ea20feecef175df相關(guān)的commit了。

此時(shí)進(jìn)行git reset --hard e63e4bc 就可以看具體的情況了。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Blob Object 10.1中說(shuō)啦,git本質(zhì)是一個(gè)內(nèi)容可尋址的系統(tǒng),所以Git的核心就是一個(gè) key-val...
    老沈Rosen閱讀 1,524評(píng)論 0 0
  • 簡(jiǎn)介 Git是一個(gè)開(kāi)源的分布式版本控制系統(tǒng),用于敏捷高效地處理任何或小或大的項(xiàng)目。 Git 與常用的版本控制工具 ...
    閩越布衣閱讀 2,875評(píng)論 0 18
  • Add & Commit git init 初始化一個(gè) Git 倉(cāng)庫(kù)(repository),即把當(dāng)前所在目錄變成...
    冬絮閱讀 5,134評(píng)論 0 9
  • 一、 Git 常用命令速查 git branch 查看本地所有分支 git status 查看當(dāng)前狀態(tài) git c...
    走在路上的日子閱讀 1,974評(píng)論 0 22
  • 一、 Git 常用命令速查 git branch 查看本地所有分支 git status 查看當(dāng)前狀態(tài) git c...
    不變禿也能變強(qiáng)閱讀 1,039評(píng)論 0 9

友情鏈接更多精彩內(nèi)容