Skip to content

Instantly share code, notes, and snippets.

@buybackoff
Last active July 12, 2020 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buybackoff/5bd478f553dfca50dbe5fd627efc2cbe to your computer and use it in GitHub Desktop.
Save buybackoff/5bd478f553dfca50dbe5fd627efc2cbe to your computer and use it in GitHub Desktop.
Keep only existing files in git history
# this doesn't support renamed/moved files
# from https://stackoverflow.com/a/17909526/801189
# Works fine on Windows with Git bash shell
$ git checkout master
$ git ls-files > keep-these.txt
$ git filter-branch --force --index-filter \
"git rm --ignore-unmatch --cached -qr . ; \
cat $PWD/keep-these.txt | tr '\n' '\0' | xargs -d '\0' git reset -q \$GIT_COMMIT --" \
--prune-empty --tag-name-filter cat -- --all
$ rm -rf .git/refs/original/
$ git reflog expire --expire=now --all
$ git gc --prune=now
# optional extra gc. Slow and may not further-reduce the repo size
$ git gc --aggressive --prune=now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment