No newline at end of file

后端 未结 12 2130

When doing a git diff it says \"No newline at end of file\".

Ok, there is no newline at end of file. What\'s the big deal?

What\'s the

12条回答
  •  有刺的猬
    2020-11-27 09:38

    It's not just bad style, it can lead to unexpected behavior when using other tools on the file.

    Here is test.txt:

    first line
    second line
    

    There is no newline character on the last line. Let's see how many lines are in the file:

    $ wc -l test.txt
    1 test.txt
    

    Maybe that's what you want, but in most cases you'd probably expect there to be 2 lines in the file.

    Also, if you wanted to combine files it may not behave the way you'd expect:

    $ cat test.txt test.txt
    first line
    second linefirst line
    second line
    

    Finally, it would make your diffs slightly more noisy if you were to add a new line. If you added a third line, it would show an edit to the second line as well as the new addition.

提交回复
热议问题