Git:错误如果 "No newline at end of file"

标签 git

git diff 中收到警告如果文本文件在文件末尾缺少换行符,这似乎是合理的。在这种情况下,有没有办法让 git 出错(在 addcommit 上),以便我更早地注意到(并可以解决)问题?

最佳答案

在 Git 中这样做的方法是使用 pre-commit hook .

你绝对可以创建一个 pre-commit hook in your Git repository to warn you about missing newline at the end of a file ,甚至还有 pre-commit hook to automatically insert a newline for you .

但是,我的建议是不要这样做,而是将文本编辑器配置为在保存文件时自动插入新行,从而完全避免了该问题。

现在大多数编辑器都支持 .editorconfig file 中定义的设置。 ,您可以在其中设置 insert_final_newline = true让文本编辑器确保文本文件总是有一个换行符。

enter image description here

关于Git:错误如果 "No newline at end of file",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60661412/

相关文章:

git - 如何忽略文件到github并包含相同的文件到heroku?

git - 使用 Git 更新 WordPress 的多个安装

git - 有什么方法可以告诉 git 忽略文件的某些行吗?

git - 在 Git 中推送提交时出现消息 'src refspec master does not match any'

git - 在 gitk 上保存 View

git - 在 git submodule/repository 中只放置一个文件

git - 使用 git,如何搜索带有 `git log` 而不是文件路径的文件模式?

git - 远程 : Invalid username or password. 致命:身份验证失败

git - 在 git 上共享 dockerfile 和共享 docker 容器有什么区别?

git - 有没有办法从本地 git 工作树中删除所有被忽略的文件?