Sometimes when I’m working with files in Git, I find myself with a change in a file that says something like
\ No newline at end of file
This happens because the file had no newline at the end of the file but when I modify the file in my text editor the newline is added on save.
Why You Should Have a Newline at the End of the File
Officially, the Posix standard defines a line as
A sequence of zero or more non- <newline> characters plus a terminating <newline> character.
How to Remove the Newline at the End of the File
Even though the newline should appear at the end of the file, sometimes I’ll opt to remove it to minimize the “noise” in a commit. I’ve found the easiest way to do this is with
perl -pi -e 'chomp if eof' <myfile>
perl -pi -e 'chomp if eof' composer.json