Created
September 15, 2016 16:08
-
-
Save tonussi/14f650e938743d4eb4d3dd3c6a805cdd to your computer and use it in GitHub Desktop.
remove git history
This file contains hidden or 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
I find that the --tree-filter option used in other answers can be very slow, especially on larger repositories with lots of commits. | |
Here is the method I use to completely remove a directory from the git history using the --index-filter option, which runs much quicker: | |
# Make a fresh clone of the repository | |
git clone ... | |
# Create tracking branches of all branches | |
for remote in `git branch -r | grep -v /HEAD`; do git checkout --track $remote ; done | |
# Remove DIRECTORY_NAME from all commits, then remove the refs to the old commits | |
# (repeat these two commands for as many directories that you want to remove) | |
git filter-branch --index-filter 'git rm -rf --cached --ignore-unmatch DIRECTORY_NAME/' --prune-empty --tag-name-filter cat -- --all | |
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d | |
# Ensure all old refs are fully removed | |
rm -Rf .git/logs .git/refs/original | |
# Perform a garbage collection to remove commits with no refs | |
git gc --prune=all --aggressive | |
# Force push all branches to overwrite their history | |
# (use with caution!) | |
git push origin --all --force | |
git push origin --tags --force | |
You can check the size of the repository before and after the gc with: | |
git count-objects -vH |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment