Fix no newline at end of file in Git diff

When you come across \ No newline at end of file, it’s because the file you’ve edited had no proper line ending set (LF or CRLF), and you, having opened it with an editor that has proper line endings set, have added a line ending.

$ git diff
diff --git a/test.txt b/test.txt
index 6a8e276..54ff075 100644
--- a/test.txt
+++ b/test.txt
@@ -1 +1 @@
-This is the content of the file
\ No newline at end of file
+This is the content of the file

However, you’ll often not want a change like this to show in a merge request because it distracts from the code and files that have genuinely been changed. So, I will usually set the file back to having no line ending, which is pretty simple to do.

In your editor, delete the last line of the file, then add it back, but use echo -n. The -n flag makes sure echo doesn’t output a new line.

echo -n "<content-of-last-line>" >> file.txt

If you run git diff again you’ll see the issue is resolved.

Alternatively if you use Vim, and notice [noeol] when you open the file, you can just :set nofixendofline and you won’t have to faff about with the above!

About

I'm a technology professional who's been passionate about computers since my Grandad introduced me to an Intel 386 back in the 90s when I was a kid. Those moments inspired a passion within for technology, and I've been playing around with anything with a circuit board ever since. Whenever I have a moment you can probably find me working on something computer-related, and this is where I like to write about those moments.