threads / discuss / 63951

git diff shows twice “\ No newline at end of file” - no need for repetition

Subject: git diff shows twice “\ No newline at end of file” - no need for repetition

## tl;dr

4 messages between Aug 13, 2025 and Aug 13, 2025.

replies: 3people: 3as markdown or json

Дилян Палаузов· Aug 13, 2025, 07:20 UTC · lore
Hello,
when a file, which does not end in a new line, is modified, git diff shows twice the text “\ No newline at end of file”. As this piece of the file is not modified, I think git diff should print it once.  In the example below the first “\ No newline at end of file” should be skipped.
I am using git 2.50.0.
As a matter of fact github also shows this information twice - https://github.com/alex-shpak/hugo-book/pull/755/files - which I find is bad.
Greetings
  Дилян
$ git diff 
diff --git a/layouts/_partials/docs/toc-show.html b/layouts/_partials/docs/toc-show.html
index 21122a1..c243ae5 100644
--- a/layouts/_partials/docs/toc-show.html
+++ b/layouts/_partials/docs/toc-show.html
@@ -2,4 +2,4 @@
   and
     (default .Site.Params.BookToC .Params.BookToC)
     (not (eq .TableOfContents "<nav id=\"TableOfContents\"></nav>"))
-) }}
\ No newline at end of file
+}}
\ No newline at end of file
Phillip Wood· Aug 13, 2025, 13:11 UTC · re: Дилян Палаузов · lore

Re: git diff shows twice “\ No newline at end of file” - no need for repetition

On 13/08/2025 08:20, Дилян Палаузов wrote:
> Hello,
> 
> when a file, which does not end in a new line, is modified, git diff shows twice the text “\ No newline at end of file”. As this piece of the file is not modified, I think git diff should print it once.  In the example below the first “\ No newline at end of file” should be skipped.

The "\ No newline at end of file" applies to the line that precedes it. Removing the first instance in your example would mean the the newline at the end of the file had been removed when the line was changed. The output of git matches what GNU diff shows for incomplete lines.

Thanks
Phillip
Show 21 quoted lines
> I am using git 2.50.0.
> 
> As a matter of fact github also shows this information twice - https://github.com/alex-shpak/hugo-book/pull/755/files - which I find is bad.
> 
> Greetings
>    Дилян
> 
> $ git diff
> diff --git a/layouts/_partials/docs/toc-show.html b/layouts/_partials/docs/toc-show.html
> index 21122a1..c243ae5 100644
> --- a/layouts/_partials/docs/toc-show.html
> +++ b/layouts/_partials/docs/toc-show.html
> @@ -2,4 +2,4 @@
>     and
>       (default .Site.Params.BookToC .Params.BookToC)
>       (not (eq .TableOfContents "<nav id=\"TableOfContents\"></nav>"))
> -) }}
> \ No newline at end of file
> +}}
> \ No newline at end of file
> 
Junio C Hamano· Aug 13, 2025, 14:51 UTC · re: Phillip Wood · lore

Re: git diff shows twice “\ No newline at end of file” - no need for repetition

Phillip Wood <phillip.wood123@gmail.com> writes:
Show 15 quoted lines
> On 13/08/2025 08:20, Дилян Палаузов wrote:
>> Hello,
>> when a file, which does not end in a new line, is modified, git diff
>> shows twice the text “\ No newline at end of file”. As this piece
>> of the file is not modified, I think git diff should print it once.
>> In the example below the first “\ No newline at end of file”
>> should be skipped.
>
> The "\ No newline at end of file" applies to the line that precedes
> it. Removing the first instance in your example would mean the the
> newline at the end of the file had been removed when the line was
> changed. The output of git matches what GNU diff shows for incomplete
> lines.
>
> Thanks

Thanks for a concise and accurate diagnosis. It is unfortunate that we cannot point at POSIX X-<.

Junio C Hamano· Aug 13, 2025, 14:42 UTC · re: Дилян Палаузов · lore

Re: git diff shows twice “\ No newline at end of file” - no need for repetition

Дилян Палаузов <dilyan.palauzov@aegee.org> writes:
> Hello,
[jc: please wrap long lines]
> when a file, which does not end in a new line, is modified, git
> diff shows twice the text “\ No newline at end of file”. As this
> piece of the file is not modified, I think git diff should print
> it once.

Perhaps "this piece of the file" is indeed modified. In your example, the file before the change had a line with ") }}" on it without terminating newline at the end, and this piece of the file is changed to a line with "}}" on it without terminating newline. The first "\ No newline" belongs to (is a part of) the file before the change and annotates something peculiar about that line, and the second "\ No newline" belongs to (is a part of) the file after the change. If the line were complete before your change and you made it incomplete while removing ") ", then the first "\ No newline" would not have been in the output. If you fixed the incomplete line at the end of file by adding a newline at the end while you removing ") ", then the second "\ No newline" would not have been in the output. But in this case, I think you had it incomplete before the change, and you did not fix it and the incompleteness of the line remained after the change, so "diff" should mark both line before and after the change incomplete with "\ No newline" marker.

> -) }}
> \ No newline at end of file
> +}}
> \ No newline at end of file

← back to recent threads