首页 > 解决方案 > 在 git diff 中避免“文件末尾没有换行符”

问题描述

我很确定我理解文件末尾没有换行符的含义。我想提供一个我很久以前创建并重新设置的分支的拉取请求(提交可能来自之前.gitattributes添加的时间)。我看到一些.java源代码文件只有变化

-}
\ No newline at end of file
+}

无论配置如何,我都希望将这些更改从 PR 提交中排除。我想避免选择更改git difftool并扩大我对git.

问题是我已经不明白这种变化是如何存在的,因为有一个.gitattributeswith

# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text
*.java text
*.css text
*.js text
*.xml text
*.dtd text
*.xsl text
*.properties text
*.txt text
*.svg text
*.yml text
*.md text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

# Denote all files that are truly binary and should not be modified.
*.png binary
*.gif binary
*.jpg binary
*.jpeg binary
*.eot binary
*.ttf binary
*.woff binary
*.woff2 binary

在存储库中,我运行git rm --cached -r . && git add .了应该将所有行尾转换为 LF 因为*.java text(也尝试过*.java text),但是没有显示任何更改(反对HEAD)并且git diff仍然显示行尾差异。

find ./ -type f -name '*.java' -exec dos2unix {} \;不会导致任何更改被识别git statusgit diff master仍然显示行结束更改(而工作目录没有暂存或未暂存的更改)。

我对隐藏更改不感兴趣,就像git diff --ignore-all-space会做的那样。

git在 Ubuntu 18.04 上使用 2.17.1。

标签: gitline-endingsgitattributeslf

解决方案


问题:\文件末尾没有换行符

$ git diff
diff --git a/abc.txt b/abc.txt
index 5996e61..6a3c3a9 100644
--- a/abc.txt
+++ b/abc.txt
{
+  **Actual change**
FR(srctxt,reptxt,fname)
else:
-    os._exit(1)
\ No newline at end of file
+    os._exit(1)

解决方案: 删除最后一行并使用echo -n "APPEND LAST LINE TEXT AFTER REMOVING THE LAST LINE TEXT" >>abc.txt

$ **echo -n "    os._exit(1)" >> abc.txt**

$ **git diff**
diff --git a/abc.txt b/abc.txt
index 5996e61..f2b2e74 100644
--- a/abc.txt
+++ b/abc.txt
@@ -419,6 +419,7 @@ EStatus 
{
+  **Actual change**
FR(srctxt,reptxt,fname)

推荐阅读