3

I'm working on bugs that will be integrated back into the original repo and a lot of the files do not have a newline at the end of the file.

Is there any way to make Git ignore this and allow me to make commits without that showing up?

I know with git diff you can do --ignore-space-at-eof to hide that but how can I make this happen when I do a git add?

The files need to be committed and pushed without adding a newline to the file as well as not have the commit show the need for a newline at the end.

I understand that there isn't a newline being added to the file but I need a way to suppress the warning message of 'no newline at end of file'

4

1 回答 1

1

文件需要提交和推送,而不需要在文件中添加换行符,也不需要提交在末尾添加换行符。

文件末尾没有换行符”消息只是一个警告。此消息不会阻止提交。

事实上,下面的代码就是这样做的。创建一个没有换行符的文件(该-n选项负责这一点)。该文件被暂存然后提交。然后,相同的文件被更改,仍然没有添加换行符,并进行了新的提交。

$ cd /d/temp
$ mkdir test && cd test
$ git init .
Initialized empty Git repository in d:/temp/test/.git/

$ echo -n test > file.txt # Create a file without a trailing newline 
$ git add .
$ git commit -m "Initial commit"
[master (root-commit) b99fc8b] Initial commit
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt

$ echo -n ing... >> file.txt # Append some text to the same file (still without a newline)
$ git add .
$ git commit -m "Fixing bug without adding a trailing newline"
[master f0be668] Fixing bug without adding a trailing newline
 1 file changed, 1 insertion(+), 1 deletion(-)

查看最新的提交将显示该过程中没有添加换行符

$ git show HEAD
commit f0be6684dafa85384984e7a725c2fa854183d1d0
Author: nulltoken <who@where.com>
Date:   Sat Oct 5 12:32:08 2013 +0200

    Fixing bug without adding a trailing newline

diff --git a/file.txt b/file.txt
index 30d74d2..2ae6cf4 100644
--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-test
\ No newline at end of file
+testing...
\ No newline at end of file

所以 Git不会自动为你添加换行符。但是,如果您看到这种情况发生,很有可能您的 IDE 或文本编辑器会在保存文件之前为您执行此操作。在这种情况下,请浏览选项并进行适当的更改。

于 2013-10-05T10:47:13.297 回答