Last active
July 12, 2020 12:32
-
-
Save buybackoff/5bd478f553dfca50dbe5fd627efc2cbe to your computer and use it in GitHub Desktop.
Keep only existing files in git history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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