Skip to content

Instantly share code, notes, and snippets.

@tajpouria
Last active January 9, 2021 06:15
Show Gist options
  • Select an option

  • Save tajpouria/bc7248ede54949078a8837d601032523 to your computer and use it in GitHub Desktop.

Select an option

Save tajpouria/bc7248ede54949078a8837d601032523 to your computer and use it in GitHub Desktop.
Delete large files/directories from commit history in git repository
# 0. Check consumed disk space
git count-objects -vH
# 1. Displays all blob objects in the repository, sorted from smallest to largest.
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
# 1. Deletes files/folders from commit history
git filter-branch -f --index-filter 'git rm -rf --cached --ignore-unmatch FILE/FOLDER FILE/FOLDER' -- --all
# 2. Clean up the local repository
rm -rf .git/refs/original/
git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
# 3. Check consumed disk space again
git count-objects -vH
# 4. Push all the changes to the remote repository
git push --all --force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment