Git - Rewriting History

The Commissar Vanishes

When there are (large or unnecessary) files in your git repository that you want to remove from history you can use the follwing methods.

Typical repos shrink from about 20 MB to less than 1 MB. 1

Use git-filter-branch or install BFG.

brew install bfg

# Show sizes of files and folders
du -sh {.*,*}
# Find 10 biggest Files in Repo
# https://stackoverflow.com/questions/10622179/how-to-find-identify-large-files-commits-in-git-history#20609719
git gc
git rev-list --objects --all | grep "$(git verify-pack -v .git/objects/pack/*.idx | sort -k 3 -n | tail -10 | awk '{print$1}')"

# Delete  folders
bfg --delete-folders <path> # --no-blob-protection

# Delete Files
bfg --delete-files <path> # --no-blob-protection

# Remove reflog
git reflog expire --expire=now --all && git gc --prune=now --aggressive

# If there are unstaged changes
git rm <path> --cached
git rm -r <path> --cached

Don't forget to add the removed folders to .gitignore if they're still present.

Footnotes

  1. That statistic is pulled out of the air.