Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does git care about trailing whitespace in my files?

Tags:

git

whitespace

What business is it of whatever VCS I'm using to worry about what I put in my files? I don't get what the point here is. Is git version control or syntax checker?

like image 686
Bjorn Avatar asked Oct 17 '09 22:10

Bjorn


People also ask

How do you fix trailing white spaces?

Hover the mouse on warning in VS Code or any IDE and use quick fix to remove white spaces. Press f1 then type trim trailing whitespace .

What does trailing whitespace mean?

Trailing whitespace. Description: Used when there is whitespace between the end of a line and the newline.

What is whitespace error in git?

What are considered whitespace errors is controlled by core. whitespace configuration. By default, trailing whitespaces (including lines that solely consist of whitespaces) and a space character that is immediately followed by a tab character inside the initial indent of the line are considered whitespace errors.


1 Answers

It is because one of the very common uses for Git is sending patch series via email. Trailing spaces cause trouble in email, and are thus usually stripped out, which means any trailing spaces will be lost in the process of sending the patch via email and applying it. This in turn means that if there are trailing spaces on lines in the repo, but not in the patch being applied, you may get spurious conflicts, or extra changes that weren't intended, when applying a patch.

This pre-commit hook used to be enabled by default, but is no longer. It seems I have misremembered; it was never deliberately enabled by default. As others have pointed out, this has always been a sample pre-commit hook; it used to be disabled by not giving it the execute bit, but that's something that can get screwed up fairly easily (for instance, running under Cygwin on Windows), so in newer versions of Git (since over a year ago) the samples have been disabled by being named pre-commit.sample. You can delete or move your .git/hooks/pre-commit to prevent this hook from executing if you don't like the behavior. You should also update your Git to something more recent, as this has been fixed for quite a while.

like image 103
Brian Campbell Avatar answered Oct 11 '22 19:10

Brian Campbell