#!/bin/bash # Get the commit hashes to keep: # We want the newest commit (which removed all unwanted cruft) # We want all commits that show up in a git blame on any existing file commit_hashes=$(git rev-parse HEAD)" "$(git ls-files | xargs -I{} git blame --minimal --abbrev=40 -- {}|grep -vF '^'| awk '{print $1}' | sort -u) # Rewrite the Git history to remove obsolete commits if [ "$commit_hashes" != "" ]; then git filter-branch --commit-filter ' if echo "'"$commit_hashes"'" | grep -q "$GIT_COMMIT"; then git commit-tree "$@"; else skip_commit "$@"; fi' HEAD fi