Home / Alt manpages / git-stripspace(1)

  • git-stripspace(1)
  • User command
  • linux

Clean Commit Text Safely with git stripspace

You will finish with a repeatable way to clean commit messages, notes, tags or branch descriptions from standard input. The command removes trailing whitespace, tidies blank lines and ensures a final newline without editing the input file. Allow about ten minutes for the basic workflow. The examples use Git 2.43.0 and its matching git-man package, installed here as Debian package version 1:2.43.0-1ubuntu7.3.

1. Check the installed command

git stripspace is an ordinary, unprivileged command. It reads standard input and writes cleaned text to standard output, so start by checking that the Git executable and manual page are available:

$ git --version
git version 2.43.0
$ command -v git
/usr/bin/git
$ man git-stripspace

Checkpoint: the version should identify the Git installation you intend to use. The details below are verified against Git 2.43.0. The command's interface is small, but scripts should still be tested after a Git upgrade if exact byte-for-byte output matters.

2. Clean ordinary commit-style text

With no option, the command removes whitespace at the end of every line, reduces runs of empty lines to one, trims empty lines at the start and end, and adds a newline to the final line if it lacks one.

$ printf '  Subject with spaces   \n\n\nBody line\n' | git stripspace
Subject with spaces

Body line

The original input is not changed because it was supplied through a pipe. Redirect the result to a new file when you want to keep it:

$ git stripspace < message.txt > message.cleaned.txt
$ cmp --silent message.txt message.cleaned.txt; printf 'same bytes: %s\n' "$?"
same bytes: 1

A status of 1 here means the files differ, which is expected for noisy input. It is not an error from git stripspace. For a direct inspection, use sed -n l, which displays line endings and trailing spaces:

$ sed -n 'l' message.cleaned.txt
Subject with spaces$
$
Body line$

3. Understand what counts as blank

Lines containing whitespace are treated as empty when the command normalises the input. Multiple empty lines collapse to one blank separator. If the whole input contains only whitespace, the command produces no output:

$ printf '   \n\t\n' | git stripspace | od -An -tx1
$

The final newline rule is easy to miss in a pipeline. It applies to non-empty output, so a one-line input without a newline becomes a line terminated by \n. This makes the result suitable for metadata fields and later Unix text processing.

4. Remove comment lines deliberately

Use --strip-comments, or its short form -s, when lines beginning with the comment character should be discarded. The default comment character is #. The match is at the start of the line, after the ordinary whitespace cleanup has not been used as a way to reclassify the line.

$ printf '# generated hint\nSubject\n# another hint\nBody\n' | git stripspace --strip-comments
Subject
Body

Do not use this option on prose where a leading hash is meaningful. A Markdown heading such as # Release notes is a comment to this command and will be removed. Check the result before replacing a message or feeding it into another tool.

5. Add comment markers to every line

Use --comment-lines, or -c, to prefix every input line with the comment character and one space. Empty lines receive only the comment character. Lines are terminated with a newline in the output:

$ printf 'Subject\n\nBody' | git stripspace --comment-lines
# Subject
#
# Body

This is useful for turning text into a commented block before placing it in a Git-generated message or configuration context. It is a transformation of the stream, not an editor operation. The command does not write back to a named file unless you redirect its output.

6. Avoid the patch-cleaning trap

git stripspace is intended for metadata such as messages and descriptions. It is not the right tool for repairing whitespace errors in a patch or tracked file. Git's manual specifically points to git apply --whitespace=fix for correcting patch whitespace. That command has different input and output behaviour, so do not substitute one for the other in a script.

Also be cautious with shell redirection. This command truncates its destination before it starts writing:

$ git stripspace < message.txt > message.txt

That example can destroy the source before the command has read it. Use a temporary or clearly different destination, inspect it, then replace the original only when that is intentional:

$ git stripspace < message.txt > message.txt.cleaned
$ sed -n 'l' message.txt.cleaned
$ mv -- message.txt.cleaned message.txt

The last command is the point of no return for the old file contents. If you need an undo path, copy the original first:

$ cp --preserve=all -- message.txt message.txt.backup

Remove the backup only after checking the replacement. No step in this guide needs sudo; adding elevated privileges would not improve text processing and could create an avoidable ownership problem.

Done means

  • The installed Git version was checked before relying on exact output.
  • Input was supplied through standard input and the cleaned result was inspected on standard output or in a separate file.
  • --strip-comments was used only when leading # lines were disposable.
  • --comment-lines was treated as a formatting transformation, including its handling of empty lines.
  • Patch whitespace was left to git apply --whitespace=fix, and any replacement file has a verified recovery path.