Skip to content

Instantly share code, notes, and snippets.

@zwimer
Created November 5, 2024 07:40
Show Gist options
  • Save zwimer/5669c7fd2bc5fe7485a7764f1e31b953 to your computer and use it in GitHub Desktop.
Save zwimer/5669c7fd2bc5fe7485a7764f1e31b953 to your computer and use it in GitHub Desktop.
Truncate `git` History
#!/bin/bash -eux
# Where to truncate from
export HASH=my-hash
# Truncate history
git checkout --orphan temp "${HASH}"
git commit -m "Truncate History"
git rebase --onto temp "${HASH}" master
git branch -D temp
# Push
git push --force-with-lease
# Clean
git gc --aggressive --prune=now
git reflog expire --expire-unreachable=now --all
git gc --aggressive --prune=now
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment