Skip to content

Instantly share code, notes, and snippets.

@zoparga
Created January 25, 2023 12:01
Show Gist options
  • Save zoparga/c083837a025d35bc5167d55358a35bfa to your computer and use it in GitHub Desktop.
Save zoparga/c083837a025d35bc5167d55358a35bfa to your computer and use it in GitHub Desktop.
Remove from git commit history
# 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment