No newline at end of file

后端 未结 12 2131

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:27

    It indicates that you do not have a newline (usually '\n', aka CR or CRLF) at the end of file.

    That is, simply speaking, the last byte (or bytes if you're on Windows) in the file is not a newline.

    The message is displayed because otherwise there is no way to tell the difference between a file where there is a newline at the end and one where is not. Diff has to output a newline anyway, or the result would be harder to read or process automatically.

    Note that it is a good style to always put the newline as a last character if it is allowed by the file format. Furthermore, for example, for C and C++ header files it is required by the language standard.

    0 讨论(0)
  • 2020-11-27 09:28

    This actually does cause a problem because line endings are automatically modified dirtying files without making any changes to them. See this post for resolution.

    git replacing LF with CRLF

    0 讨论(0)
  • 2020-11-27 09:29

    Your original file probably had no newline character.

    However, some editors like gedit in linux silently adds newline at end of file. You cannot get rid of this message while using this kind of editors.

    What I tried to overcome this issue is to open file with visual studio code editor

    This editor clearly shows the last line and you can delete the line as you wish.

    0 讨论(0)
  • 2020-11-27 09:34

    The reason this convention came into practice is because on UNIX-like operating systems a newline character is treated as a line terminator and/or message boundary (this includes piping between processes, line buffering, etc.).

    Consider, for example, that a file with just a newline character is treated as a single, empty line. Conversely, a file with a length of zero bytes is actually an empty file with zero lines. This can be confirmed according to the wc -l command.

    Altogether, this behavior is reasonable because there would be no other way to distinguish between an empty text file versus a text file with a single empty line if the \n character was merely a line-separator rather than a line-terminator. Thus, valid text files should always end with a newline character. The only exception is if the text file is intended to be empty (no lines).

    0 讨论(0)
  • 2020-11-27 09:34

    For what it's worth, I encountered this when I created an IntelliJ project on a Mac, and then moved the project over to my Windows machine. I had to manually open every file and change the encoding setting at the bottom right of the IntelliJ window. Probably not happening to most if any who read this question but that could have saved me a couple of hours of work...

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题