719

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

What's the significance of the message and what's it trying to tell us?

2
  • 27
    Perhaps, if you have a file that ends without a newline, and you add another line, git would have to show that the former last line has changed, since it includes the newline character as part of the line?
    – nafg
    Apr 13, 2014 at 21:20
  • 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...
    – Lou Morda
    Nov 18, 2016 at 1:49

13 Answers 13

657

It indicates that you do not have a newline (usually \n, aka LF 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, in text files, if it is allowed by the file format. Furthermore, for example, for C and C++ header files it is required by the language standard.

15
  • 204
    Out of curiosity, can you explain why it's considered good style to always put a newline as the last character? Edit: found this discussion. Nov 16, 2012 at 20:27
  • 157
    @PaulBellora Historically, it was a decision made by the C language standard stackoverflow.com/a/729725/233098 Practically, because many Unix tools require or expect it for proper display stackoverflow.com/a/729795/233098. Philosophically, because each line in a text file terminates with an "end-of-line" character--the last line shouldn't be any exception. Thinking about it differently, let's explore the inverse. If there was a "start-of-line" marker instead of "end-of-line", would you omit the "start-of-line" character on the first line?
    – Joe
    Apr 24, 2014 at 3:50
  • 55
    @Joe That doesn't make that much sense. A newline is a new line, i.e. the separator between lines, not an end-of-line. We don't have start of line characters because they're not necessary. We don't have end of line characters for the same reason.
    – acjay
    Sep 19, 2014 at 1:59
  • 13
    @acjay I argue that there's inherently better between "Separator between lines" vs "end-of-line". Neither view is inherently right or wrong, just one way to look at it. I'm suggesting we continue to use the point-of-view that's historically practical, since we're already doing it that way and it does make sense when you accept it. Consistency is important. There's no need to break that in the name of "the separator between lines" viewpoint.
    – Joe
    Sep 19, 2014 at 16:18
  • 30
    @WORMSS "New to me" isn't the same thing as "a new convention." This is just like discovering any other kind of programming convention. You just go with it. You could deviate, but you're only isolating yourself. (Or in this case, actually breaking tools.) Think about how many others discovered some Rails convention, or PEP8, and how consistent those communities remained as a whole because they did give in--despite having written code in the contrary.
    – Joe
    Dec 18, 2014 at 1:42
159

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.

4
  • 16
    The result of cat is ok but the wc parameter "-l, --lines" is is just wrong. Even it's manual says "print the newline counts" and not "print the line counts". Feb 13, 2019 at 7:20
  • 2
    And I cannot even reproduce this (wc and cat) with recent util linux (util-linux 2.34).
    – wget
    Oct 1, 2019 at 10:40
  • 4
    @wget I'm on util-linux 2.34 and it can confirm that what this answer describes is the current behaviour. My guess is that your editor added the "\n" character.
    – stephanos
    Dec 27, 2019 at 11:36
  • @TheincredibleJan Every lines ends with a '\n'. It is not a line if there is no '\n'. The -l --lines argument is absolutely correct. Mar 4, 2021 at 18:16
52

The only reason is that Unix historically had a convention of all human-readable text files ending in a newline. At the time, this avoided extra processing when displaying or joining text files, and avoided treating text files differently to files containing other kinds of data (eg raw binary data which isn't human-readable).

Because of this convention, many tools from that era expect the ending newline, including text editors, diffing tools, and other text processing tools. Mac OS X was built on BSD Unix, and Linux was developed to be Unix-compatible, so both operating systems have inherited the same convention, behaviour and tools.

Windows wasn't developed to be Unix-compatible, so it doesn't have the same convention, and most Windows software will deal just fine with no trailing newline.

But, since Git was developed for Linux first, and a lot of open-source software is built on Unix-compatible systems like Linux, Mac OS X, FreeBSD, etc, most open-source communities and their tools (including programming languages) continue to follow these conventions.

There are technical reasons which made sense in 1971, but in this era it's mostly convention and maintaining compatibility with existing tools.

2
  • 7
    Your answer implies that there are only historical reasons for it. This is false. The same reasons that applied in 1971 do mostly still apply today. The other answers give more details Mar 4, 2021 at 18:15
  • 3
    personally not convinced by the other answers May 23, 2022 at 23:24
51

If you add a new line of text at the end of the existing file which does not already have a newline character at the end, the diff will show the old last line as having been modified, even though conceptually it wasn’t.

This is at least one good reason to add a newline character at the end.

Example

A file contains:

A() {
    // do something
}

Hexdump:

00000000: 4128 2920 7b0a 2020 2020 2f2f 2064 6f20  A() {.    // do 
00000010: 736f 6d65 7468 696e 670a 7d              something.}

You now edit it to

A() {
    // do something
}
// Useful comment

Hexdump:

00000000: 4128 2920 7b0a 2020 2020 2f2f 2064 6f20  A() {.    // do 
00000010: 736f 6d65 7468 696e 670a 7d0a 2f2f 2055  something.}.// U
00000020: 7365 6675 6c20 636f 6d6d 656e 742e 0a    seful comment..

The git diff will show:

-}
\ No newline at end of file
+}
+// Useful comment.

In other words, it shows a larger diff than conceptually occurred. It shows that you deleted the line } and added the line }\n. This is, in fact, what happened, but it’s not what conceptually happened, so it can be confusing.

5
  • 6
    We can write the same thing in the other direction : If you remove a new line at the end of the existing file which is having a newline at the end already, the diff will show old last line also as modified, when conceptually its not. At least one good reason to remove a newline at the end.
    – gentiane
    Sep 8, 2016 at 9:12
  • 7
    @gentiane You're confusing "a new line" (a new line) and "a newline" (1 or 2 characters delimiting the end of a line)
    – minexew
    Jan 6, 2017 at 17:00
  • 1
    @minexew No, gentiane isn't. Maybe you just do not realize that "a new line" is the same as "a newline". Feb 13, 2019 at 7:24
  • 8
    @TheincredibleJan The way they're used in the answer, the two terms have distinct meanings. I don't know if you're trying to be a smart-ass or are just misunderstanding what's going on.
    – minexew
    Feb 14, 2019 at 21:59
  • 1
    @minexew I don't understand you and gentiane is right. Oct 13, 2020 at 7:11
19

It just indicates that the end of the file doesn't have a newline. It's not a catastrophe it's just a message to make it clearer that there isn't one when looking at a diff in the command line.

1
  • It can cause problems. For example in case this file ends up puppet manifest template that sets up sudoers file. Sudoers file cannot be "noeol", it will cause syntax errors. But generally it is not catastrophe.
    – Kris Avi
    Sep 1 at 11:53
19

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).

2
  • 3
    Why am I downvoted -2? I pointed out not only confirmation of what other answers have stated (i.e. standard UNIX-based tools expect a newline as a terminator for lines) but also that there is no way to distinguish an empty file from a single empty line, which is absolutely true. I specifically answered the original question "What's the significance of the message and what's it trying to tell us?" Sep 14, 2018 at 8:31
  • 1
    I did not downvote you but this response seems to be specific to Unix type systems in that it only applies when a newline is just the newline character. It is not clear that that applies here. Also, the warning seems useless if the file consists of just an empty line. However I avoid Stackoverflow because people often downvote without an explanation.
    – Sam Hobbs
    Apr 13, 2019 at 22:54
12

There is one thing that I don't see in previous responses. Warning about no end-of-line could be a warning when a portion of a file has been truncated. It could be a symptom of missing data.

1
  • Good point in general, but I don't think it makes sense in the context of this particular question.
    – cst1992
    Apr 9, 2019 at 13:10
7

The core problem is what you define line and whether end-on-line character sequence is part of the line or not. UNIX-based editors (such as VIM) or tools (such as Git) use EOL character sequence as line terminator, therefore it's a part of the line. It's similar to use of semicolon (;) in C and Pascal. In C semicolon terminates statements, in Pascal it separates them.

5

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

4

Source files are often concatenated by tools (C, C++: header files, Javascript: bundlers). If you omit the newline character, you could introduce nasty bugs (where the last line of one source is concatenated with the first line of the next source file). Hopefully all the source code concat tools out there insert a newline between concatenated files anyway but that doesn't always seem to be the case.

The crux of the issue is - in most languages, newlines have semantic meaning and end-of-file is not a language defined alternative for the newline character. So you ought to terminate every statement/expression with a newline character -- including the last one.

5
  • 1
    In C/C++ you could write your whole project in one line. No need for newline. Feb 13, 2019 at 7:28
  • 1
    You could write your whole project in one line... if you do not use a // style comment in the middle of the code. Feb 18, 2020 at 21:10
  • @TheincredibleJan Not true, #include's and other preprocessor statements need to be on a line by themselves.
    – Yun
    Feb 7, 2022 at 15:37
  • @Yun That's true but do you really consider the preprocessor to be part of the language? I don't. Correct me if I'm wrong. :) Feb 16, 2022 at 11:36
  • @TheincredibleJan I do. The C language specification documents (stackoverflow.com/questions/81656/…) have a section on preprocessing directives.
    – Yun
    Feb 17, 2022 at 19:50
3
ubuntu$> vi source.cpp

:set binary noeol
0
2

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.

1

What

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

In some ways, yes, but it's more nuanced and subtle.

When doing a git diff, git shows the difference between versions of files, displayed as chunks where those files differ. This may include a chunk at the end of the file.

If both versions of the file do not end in a newline, the end of the chunk will read

\ No newline at the end of file

whereas if only one version don't end in a newline, the chunk will end in either

-last line
\ No newline at the end of file
+new last line

or

-last line
+new last line
\ No newline at the end of file

Why?

What's the significance of the message

The reason is simple. One of the main purpose of git diff is display changes, unambiguously so that it can be used as a input to git apply. To do this, Git needs to know what it is supposed to do with newlines when applying a diff. Should it remove, keep or change them?

The \ No newline at the end of file is a way of doing that. It's also helpful to humans that want to be aware of such changes, because in some instances having or not having newlines is important to the file.

and what's it trying to tell us?

Well, just that there is no newline at the end of this file. Or that there wasn't but now there is. Or that there was but now isn't.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.