0

I'm writing a README.md. I've added an extra space at the end of some lines. When I do git diff shows this as highlighted as below

Git warning on extra space after a line

Why the trailing whitespace is considered an error?

Ganesh Satpute
  • 3,664
  • 6
  • 41
  • 78
  • As mentioned in the last line. Are these errors or warnings? What is the reason for highlighting them? – Ganesh Satpute Jan 02 '20 at 09:36
  • These are not the difference between working tree and HEAD. These are all new lines. – Ganesh Satpute Jan 02 '20 at 10:07
  • 1
    Does this answer your question? [Why is it bad to commit lines with trailing whitespace into source control?](https://stackoverflow.com/questions/300489/why-is-it-bad-to-commit-lines-with-trailing-whitespace-into-source-control) – phd Jan 02 '20 at 13:58

2 Answers2

1

One reason I can think of is the line ending styles for git.

The question git diff - show me line ending changes? asked can be of some help as well.

Hemant Kumar
  • 186
  • 6
  • Sorry, if I missed something but that answer talks about Linux vs Windows line endings. In my case, the error is about the extra space at the end of the line. – Ganesh Satpute Jan 02 '20 at 09:35
  • This might happen in case code is earlier checked in with different line endings and you have a different line ending styles, then you might face the problem. eg. if the same code is committed on Linux machine with Linux end styles and you are using windows machine with windows style line ending then this issue is faced. However, I think @Schwern has a better explanation than mine. – Hemant Kumar Jan 02 '20 at 11:26
  • I'm telling you it's not about Linux or Windows file endings – Ganesh Satpute Jan 02 '20 at 13:03
1

Git will show you "common whitespace problems" such as trailing whitespace. You can control this behavior with the core.whitespace configuration option. -trailing-space will turn this highlighting off.

[core]
        whitespace = -trailing-space

I've never understood why this is considered a problem, but many people do, so I recommend stripping them off. You can configure your editor to strip trailing whitespace for you. If doing this for all documents doesn't appeal to you, use EditorConfig to provide universal editor configurations for your projects.

root = true

[*]
trim_trailing_whitespace = true
insert_final_newline = true

See also

Schwern
  • 153,029
  • 25
  • 195
  • 336
  • Thanks for your answer @Schwern. Is trailing whitespace a problem? In what use cases it could cause issues? – Ganesh Satpute Jan 02 '20 at 09:41
  • 1
    @GaneshSatpute: trailing whitespace annoys your co-workers. Unless of course it doesn't. (In other words, it's a personal preference thing.) – torek Jan 02 '20 at 09:44
  • 1
    @GaneshSatpute I've been using Git for over 10 years and I've never heard a good explanation for why trailing whitespace is a problem. But so many people seem to think it's a problem (possibly because of Git highlighting it) that I strip it out to avoid time consuming style arguments. [EditorConfig](https://editorconfig.org/) makes it easy. – Schwern Jan 02 '20 at 09:44
  • Thanks for the explanation @Schwern. I agree with you. – Ganesh Satpute Jan 02 '20 at 10:06