git stripspace is a small Git command that cleans text the same way Git cleans commit messages, tag messages, and notes.

With no flags it:


Important: it reads from stdin and writes to stdout — it does not take filenames (so git stripspace a.py won’t work).

Usage:

git stripspace [-s | --strip-comments]
git stripspace [-c | --comment-lines]

Example (with $ indicating end of line):

Note: the leading | and trailing $ are just visual markers (they are not part of the file). To render any file that way:

$ sed 's/$/$/' noisy.txt | sed 's/^/|/'

Noisy input:

|A brief introduction   $
|   $
|$
|A new paragraph$
|# with a commented-out line    $
|explaining lots of stuff.$
|$
|# An old paragraph, also commented-out. $
|      $
|The end.$
|  $

Run it:

$ git stripspace < noisy.txt

Output:

|A brief introduction$
|$
|A new paragraph$
|# with a commented-out line$
|explaining lots of stuff.$
|$
|# An old paragraph, also commented-out.$
|$
|The end.$

Strip comment lines:

$ git stripspace --strip-comments < noisy.txt

Output:

|A brief introduction$
|$
|A new paragraph$
|explaining lots of stuff.$
|$
|The end.$

Print the cleaned version to stdout:

$ git stripspace < a.md

Overwrite a file safely:

$ tmp="$(mktemp)"
$ git stripspace < a.md > "$tmp" && mv "$tmp" a.md

If you’re cleaning something like .git/COMMIT_EDITMSG, use --strip-comments to drop lines that start with #:

$ git stripspace --strip-comments < .git/COMMIT_EDITMSG


Note: this is for cleaning Git “metadata” text. If you’re fixing whitespace in patches/files, prefer git apply --whitespace=fix.