Git: error if "No newline at end of file"

Viewed 3196

One gets a warning in git diff if a text file lacks a newline at the end of the file, seems reasonable. Is there a way to get git to error (on add or commit say) in this case, just so that I notice (and can fix) the problem earlier?

1 Answers

The way to do that in Git would be to use a pre-commit hook.

You can definitely create a pre-commit hook in your Git repository to warn you about missing newline at the end of a file, or even have a pre-commit hook to automatically insert a newline for you.

However, my advice would be to not do that, and instead configure your text editor to automatically insert the new line whenever you save the file, which avoids the issue entirely.

Most editors nowadays support settings defined in an .editorconfig file, where you can set insert_final_newline = true to have text editors ensure text files will always have a newline.

enter image description here

Related