Skip to content

Instantly share code, notes, and snippets.

@syntaxval
Last active September 28, 2018 07:43
Show Gist options
  • Save syntaxval/2d29ea4cf00040cb869f05e47e5e8ac8 to your computer and use it in GitHub Desktop.
Save syntaxval/2d29ea4cf00040cb869f05e47e5e8ac8 to your computer and use it in GitHub Desktop.
Git History Rewrite
# Remove file
git filter-branch --tree-filter 'rm path/to/file' HEAD
# Remove directory
git filter-branch --tree-filter 'rm -rf path/to/directory' --prune-empty HEAD
# Replace a file
git filter-branch --tree-filter 'cp /absolute/path/to/file path/to/file' HEAD
# GPG sign all rewritten history.
git filter-branch -f --commit-filter 'git commit-tree -S "$@";' -- --all
# Cleanup
git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now
# Force commit after history has been rewriten.
git push origin master --force
@syntaxval
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment