我有一个300mb的git回购。我目前签出的文件的总大小是2 MB,其余的git回购的总大小是298 MB。这基本上是一个只有代码的回购,不应该超过几MB。
我怀疑有人不小心提交了一些大文件(视频、图像等),然后删除了它们……但不是从git,所以历史仍然包含无用的大文件。如何在git历史中找到大文件?有400多个提交,所以一个接一个的提交是不实际的。
注意:我的问题不是关于如何删除文件,而是如何在第一时间找到它。
我有一个300mb的git回购。我目前签出的文件的总大小是2 MB,其余的git回购的总大小是298 MB。这基本上是一个只有代码的回购,不应该超过几MB。
我怀疑有人不小心提交了一些大文件(视频、图像等),然后删除了它们……但不是从git,所以历史仍然包含无用的大文件。如何在git历史中找到大文件?有400多个提交,所以一个接一个的提交是不实际的。
注意:我的问题不是关于如何删除文件,而是如何在第一时间找到它。
当前回答
如果你在Windows上,下面是一个PowerShell脚本,它将打印存储库中最大的10个文件:
$revision_objects = git rev-list --objects --all;
$files = $revision_objects.Split() | Where-Object {$_.Length -gt 0 -and $(Test-Path -Path $_ -PathType Leaf) };
$files | Get-Item -Force | select fullname, length | sort -Descending -Property Length | select -First 10
其他回答
如何在git历史记录中追踪大文件?
从分析、确认和选择根本原因开始。使用git-repo-analysis来提供帮助。
你也可以在BFG Repo-Cleaner生成的详细报告中找到一些价值,它可以通过克隆到数字海洋液滴,使用10MiB/s的网络吞吐量快速运行。
我在苏黎世联邦理工学院物理系的维基页面上找到了一个简单的解决方案(接近该页的末尾)。只要做个git垃圾收集,把垃圾清除掉,然后
git rev-list --objects --all \
| grep "$(git verify-pack -v .git/objects/pack/*.idx \
| sort -k 3 -n \
| tail -10 \
| awk '{print$1}')"
将为您提供存储库中最大的10个文件。
现在还有一个更懒的解决方案,GitExtensions现在有一个插件,可以在UI中做到这一点(以及处理历史重写)。
我发现这个脚本在过去在git存储库中查找大型(和不明显的)对象非常有用:
http://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
#!/bin/bash
#set -x
# Shows you the largest objects in your repo's pack file.
# Written for osx.
#
# @see https://stubbisms.wordpress.com/2009/07/10/git-script-to-show-largest-pack-objects-and-trim-your-waist-line/
# @author Antony Stubbs
# set the internal field separator to line break, so that we can iterate easily over the verify-pack output
IFS=$'\n';
# list all objects including their size, sort by size, take top 10
objects=`git verify-pack -v .git/objects/pack/pack-*.idx | grep -v chain | sort -k3nr | head`
echo "All sizes are in kB's. The pack column is the size of the object, compressed, inside the pack file."
output="size,pack,SHA,location"
allObjects=`git rev-list --all --objects`
for y in $objects
do
# extract the size in bytes
size=$((`echo $y | cut -f 5 -d ' '`/1024))
# extract the compressed size in bytes
compressedSize=$((`echo $y | cut -f 6 -d ' '`/1024))
# extract the SHA
sha=`echo $y | cut -f 1 -d ' '`
# find the objects location in the repository tree
other=`echo "${allObjects}" | grep $sha`
#lineBreak=`echo -e "\n"`
output="${output}\n${size},${compressedSize},${other}"
done
echo -e $output | column -t -s ', '
这将给你blob的对象名称(SHA1sum),然后你可以使用这样的脚本:
哪个提交有这个斑点?
…来查找指向这些blob的提交。
像这样使用git-filter-repo的——analyze特性:
$ cd my-repo-folder
$ git-filter-repo --analyze
$ less .git/filter-repo/analysis/path-all-sizes.txt
试试git ls-files | xargs du -hs——threshold=1M。
我们在CI管道中使用下面的命令,如果它在git repo中发现任何大文件,它就会停止:
test $(git ls-files | xargs du -hs --threshold=1M 2>/dev/null | tee /dev/stderr | wc -l) -gt 0 && { echo; echo "Aborting due to big files in the git repository."; exit 1; } || true