git - 如何阻止 Git 在本地文件中插入回车符?

标签 git newline line-breaks carriage-return line-endings

我一直在尝试很多不同的配置来试图阻止 Git 从插入 CR(回车)。 我有一个本地文件,其源代码中有 LF(不是 CRLF)行结尾 repo :

$ cat -vT  Source/watchr.bat
echo OFF
echo ==========================================================
echo ==========================================================
echo The "watchr" command is deprecated.
echo The new command is sidekick.bat
echo " _
echo "| |
echo "| |_ _   _ _ __   ___

注意:没有^M,所以里面没有CR。
这是大约 80 个文件中的一个,因此提交将创建大量文件 Git 历史中不必要的改动。

现在查看 git diff 的输出:

$ gd -R Source/watchr.bat
+echo OFF^M
+echo ==========================================================^M
+echo ==========================================================^M
+echo The "watchr" command is deprecated.^M
+echo The new command is sidekick.bat^M
+echo " _                      ^M
+echo "| |                    ^M
+echo "| |_ _   _ _ __   ___ ^M

啊,^M 每行。为什么?怎么办?

设置:

$ git config --global core.autocrlf
true

$ git config  core.autocrlf
false

$ cat -vT .gitattributes
# Set default behavior to automatically normalize line endings.
* text=

将设置更改为input(或false)和auto (.gitattributes) 无效。
Git 仍然想在 watchr.bat 文件中插入一个 CR。
我的主目录中的 .gitconfig 也有 autocrlf = true

如何阻止 Git 执行此操作?

平台:Git 版本 1.9.5.msysgit.0,Windows 7。

最佳答案

你应该尝试:

git config --global core.autocrlf false

(这是我在“Git on Windows (msysgit) - Unix or DOS line termination”中推荐的)

同时检查本地配置:

git config core.autocrlf false

确保您没有 .gitattribute with a core.eol directive这也可以添加 \r\n eol.

关于git - 如何阻止 Git 在本地文件中插入回车符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27894029/

相关文章:

git - 共享 Git 存储库中的 .editorconfig

PHP 正则表达式将新行限制为最多两个

java - 在java中读取文本文件,想检查它使用什么样的换行符

javascript - 如何检测文本区域输入中的换行符?

Git:公钥认证失败

linux - `ssh -vT git@github.com` 工作正常,代理在 github ssh key 中。问题在哪里?

newline - 模仿 Markdown 中的换行符

javascript - 在HTML中用JS输入NewLine

javascript - 使用 mailto 时保留文本区域中的换行符

linux - 从 GitHub 迁移到 GitLab(作为 POSIX 环境中的远程)