Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save daragh/6973630 to your computer and use it in GitHub Desktop.
Save daragh/6973630 to your computer and use it in GitHub Desktop.
A shell function to remove trailing whitespace from changed lines in a git commit. Intended to be used as part of a git pre-commit hook.
remove_trailing_whitespace_from_changed_lines() {
patch=$(mktemp --tmpdir remove-trailing-whitespace-patch-XXXXX)
git diff --cached --no-color --diff-filter=M > "$patch"
git apply --index --reverse "$patch"
git apply --index --whitespace=fix "$patch"
rm "$patch"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment