Use git to remove/delete a large file from commit history in Git repository

less than 1 minute read

Published:

1. At the very beginning, you can use the following command to find the large files in your commit history

git rev-list --objects --all \
| git cat-file --batch-check='%(objecttype) %(objectname) %(objectsize) %(rest)' \
| awk '/^blob/ {print substr($0,6)}' \
| sort --numeric-sort --key=2 \
| cut --complement --characters=13-40 \
| numfmt --field=2 --to=iec-i --suffix=B --padding=7 --round=nearest

 

2. For example, you found the large files are end with .db, you can use the following command to delete all large files which were end with .db

git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch *.db' --prune-empty --tag-name-filter cat -- --all

3. In this step, you can use the following command to clean up the local repository

rm -rf .git/refs/original/
git reflog expire --expire=now --all
git fsck --full --unreachable
git repack -A -d
git gc --aggressive --prune=now

4. Update your commit

git push origin --force –all

Leave a Comment