簡體   English   中英

在提交時讓Git忽略“文件末尾沒有換行符”

[英]Make Git ignore 'No newline at end of file' when committing

我正在研究將被集成回原始倉庫的bug,並且很多文件在文件末尾沒有換行符。

有沒有辦法讓Git忽略這一點並允許我在沒有出現的情況下進行提交?

我知道使用git diff你可以做--ignore-space-at-eof來隱藏它但是當我做一個git add時我該如何實現呢?

需要提交和推送文件而不向文件添加換行符,也不要讓提交在最后顯示需要換行符。

我知道文件中沒有添加換行符,但我需要一種方法來抑制“文件末尾沒有換行符”的警告信息

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

“文件末尾沒有換行符”消息只是一個警告。 此消息不會阻止提交。

實際上,下面的代碼就是這樣做的。 創建一個沒有換行符的文件( -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或文本編輯器很可能會為您執行此操作。 在這種情況下,瀏覽選項並進行適當的更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM