Git Reference ยท Plumbing Commands
git stripspace command
Reference for git stripspace: trim trailing whitespace, collapse blank lines, strip or add comment lines and clean commit messages with -s and -c.
git stripspace is a text filter: it reads standard input, tidies it the way Git tidies commit messages, and writes the result to standard output. It removes trailing whitespace from each line, squeezes runs of blank lines into one, drops blank lines at the start and end, and makes sure the text ends with a single newline.
Git applies the same cleanup to a message you type in the editor. Calling the command yourself is useful in hooks, in scripts that generate commit messages, and anywhere you want to normalize text before storing it.
git stripspace [-s | --strip-comments] < input git stripspace [-c | --comment-lines] < input
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Syntax
git stripspace git stripspace -s git stripspace -c
| Name | Description |
|---|---|
| (no option) | Remove trailing whitespace on every line, collapse consecutive empty lines, remove leading and trailing empty lines, and end the text with exactly one newline. |
-s, --strip-comments |
Do all of the above and also delete lines that begin with the comment character (# by default). |
-c, --comment-lines |
Prefix each line with the comment character and a space. Empty lines become a bare comment character, so no trailing space is added. |
Input and output: the command is a filter and takes no file arguments. If the input is empty or only whitespace, the output is empty (not even a newline).
Exit status: 0 normally. It never fails on unusual text.
Usage notes
- Comment character. It comes from
core.commentChar, which can be a single character, orautoto let Git choose one that does not begin any line of the message.-sand-cuse that setting (newer Git versions also accept a string viacore.commentString). - Only whitespace at the end of lines is removed. Leading spaces and indentation stay, which preserves indented code blocks in a message.
- Comments and blank lines. With
-s, comment lines are dropped, and blank lines that end up next to each other are collapsed too. - A hash character in the middle of a line is not a comment. Only lines that start with the comment character count. A message line like
#42 fixes the bugis removed by-sfor the same reason, so reference issues asFixes #42or change the comment character. - Used by commit.
git commit --cleanup=strip(the default when the message is edited) applies-s;--cleanup=whitespacekeeps comments but still cleans spacing;--cleanup=verbatimdoes nothing. - Handy in hooks.
commit-msghooks often pipe the message file throughgit stripspacebefore checking its length or format. - Not for source files. It treats the whole input as prose and merges blank lines, so do not run it on code where blank lines have meaning. Use
git diff --checkor a formatter instead.
Related
- git interpret-trailers, git config, git status
- Lessons: Commits, Hooks