How to remove all files from Git LFS quickly.
Prerequisites⌗
- A Git repo with files stored using Git LFS
- The BFG Repo Cleaner tool (requires java itself)
- Have a clean HEAD
The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history…
The One-Liner⌗
git lfs ls-files --all | awk '{split($0,a," - ");l=split(a[2],b,"/"); print b[l]}' | xargs -d\\n -n1 java -jar ../bfg.jar -D && \
git reflog expire --expire=now --all && \
git gc --prune=now --aggressive
What’s Happening?⌗
git lfs ls-files --all
-> list all files & hashesawk '{split($0,a," - ");l=split(a[2],b,"/"); print b[l]}'
-> split the data to only get file names, since bfg does not work with pathsxargs -d\\n -n1 java -jar ../bfg.jar -D
-> for each file emitted by awk, run bfg with--delete-files/-D
(removes the file)git reflog expire --expire=now --all
-> The “expire” subcommand prunes old reflog entries.git gc --prune=now --aggressive
->git-gc
cleans up unnecessary files and optimizes the local repository.--prune=now
prunes loose objects older than now (all loose),--aggressive
forcesgit-gc
to optimize “heavier”.
Read other posts