Clean Commit Text Safely with git stripspace
Trailing spaces and ragged blank lines in a commit message are exactly what git stripspace fixes, in one pass, without touching your input file. You will finish with a repeatable way to clean commit messages, notes, tags or branch descriptions from standard input. 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.
The route
Jump straight to the step you need, or tick off Done means at the end.
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 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 interface is small, but test scripts again 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 unchanged, since it came in through a pipe. Redirect the result to a new file 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, 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 is 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 with no newline becomes a line terminated by \n. That 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 #, matched at the start of the line, after ordinary whitespace cleanup:
$ printf '# generated hint\nSubject\n# another hint\nBody\n' | git stripspace --strip-comments
Subject
Body
Tip
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 get only the comment character, and every line is newline-terminated 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 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 that. That command has different input and output behaviour, so do not substitute one for the other in a script.
Warning
Shell redirection 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
Recovery
That last command is the point of no return for the old file contents. Copy the original first if you need an undo path:
$ cp --preserve=all -- message.txt message.txt.backup
Remove the backup only after checking the replacement. No step in this guide needs sudo; elevated privileges would not improve text processing and could create an avoidable ownership problem.
Done means
- Checked the installed Git version before relying on exact output.
- Fed input through standard input and inspected the cleaned result on standard output or in a separate file.
- Used
--strip-commentsonly when leading#lines were disposable. - Treated
--comment-linesas a formatting transformation, including its handling of empty lines. - Left patch whitespace to
git apply --whitespace=fix, with a verified recovery path for any replacement file.