08月22, 2020

清除git仓库中的垃圾文件

当在git仓库中提交了上百兆的二进制文件,比如图片、视频、zip等,.git/objects目录下的日志文件就会变得非常庞大,即使后来commit删除了这些大文件,日志文件仍然很大,不利于多个环境协同工作

经过尝试,找到了一种法可以找到git目录下的大文件,并删除它

手式清除

# find biggest files

find .git/objects -size +10M

To see the 10 biggest files, run this from the root directory:

git verify-pack -v [your biggest file in previous step] \
| sort -k 3 -n \
| tail -10

To see what each file is, run this:

git rev-list --objects --all | grep [first few chars of the sha1 from previous output]

Most of the files are .png, and the last one in the list is a .mov, which I would guess takes up most of the space. There are also .csv and .pdf files. The next step would be to clean up your git by removing all of those unnecessary files.

you could do it manually following this git article, as outlined below:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch *.mov' -- --all
rm -Rf .git/refs/original
rm -Rf .git/logs/
git gc --aggressive --prune=now

then verify, and commit your changes

git count-objects -v
git push --force

清除全部历史commit

## This script is used to clean all git commit
if [[ "$1" = 'all' ]];then
    echo "Clean all git commit"
    git checkout --orphan latest_branch
    git add -A
    git commit -am "Delete all previous commit"
    git branch -D master
    git branch -m master
fi

echo "Cleanup refs and logs"
rm -Rf .git/refs/original
rm -Rf .git/logs/

echo "Cleanup unnecessary files"
git gc --aggressive --prune=now

echo "Prune all unreachable objects"
git prune --expire now

#git push -f origin master

https://github.com/ahuigo/a/blob/master/tool/gitclean.sh

本文链接:http://57km.cc/post/gabage collection for git.html

-- EOF --

Comments