Prerequisites

  1. A Git repo with files stored using Git LFS
  2. The BFG Repo Cleaner tool (requires java itself)
  3. 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?

  1. git lfs ls-files --all -> list all files & hashes
  2. awk '{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 paths
  3. xargs -d\\n -n1 java -jar ../bfg.jar -D -> for each file emitted by awk, run bfg with --delete-files/-D (removes the file)
  4. git reflog expire --expire=now --all -> The “expire” subcommand prunes old reflog entries.
  5. 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 forces git-gc to optimize “heavier”.