如何强制 git 在 windows 下使用 LF 而不是 CR+LF?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2517190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I force git to use LF instead of CR+LF under windows?
提问by sorin
I want to force git to checkout files under Windows using just LF
not CR+LF
. I checked the two configuration options but I was not able to find the right combination of settings.
我想强制 git 在 Windows 下使用LF
not签出文件CR+LF
。我检查了两个配置选项,但找不到正确的设置组合。
I want it to convert all files to LF
and keep the LF
on the files.
我希望它将所有文件转换为LF
并保留LF
在文件上。
Remark: I used autocrlf = input
but this just repairs the files when you commit them. I want to force it to get them using LF
.
备注:我使用过,autocrlf = input
但这只是在您提交文件时修复文件。我想强制它让他们使用LF
.
Probably I wasn't so clear: the repository is already using LF
but the files checked out using msysgit are using CR+LF
and I want to force msysgit to get them with LF
: forcing Unix line endings.
可能我不是很清楚:存储库已经在使用,LF
但是使用 msysgit 检出的文件正在使用CR+LF
,我想强制 msysgit 获取它们LF
:强制 Unix 行结尾。
>git config --list | grep crlf
core.autocrlf=input
采纳答案by VonC
The OP added in his question:
OP在他的问题中补充道:
the files checked out using msysgit are using
CR+LF
and I want to force msysgit to get them withLF
使用 msysgit 检出的文件正在使用
CR+LF
,我想强制 msysgit 获取它们LF
A first simple step would still be in a .gitattributes
file:
第一个简单的步骤仍然是在一个.gitattributes
文件中:
# 2010
*.txt -crlf
# 2020
*.txt text eol=lf
(as noted in the commentsby grandchild, referring to .gitattributes
End-of-line conversion), to avoid any CRLF
conversion for files with correct eol
.
(如在评论指出由孙子,参照.gitattributes
结束行变换),以避免任何CRLF
与正确的文件的转换eol
。
And I have always recommendedgit config --global core.autocrlf false
to disable any conversion (which would apply to allversioned files)
而且我一直建议git config --global core.autocrlf false
禁用任何转换(这将适用于所有版本化文件)
See Best practices for cross platform git config?
Since Git 2.16(Q1 2018), you can use git add --renormalize .
to apply those .gitattributes
settings immediately.
从 Git 2.16(2018 年第一季度)开始,您可以立即git add --renormalize .
应用这些.gitattributes
设置。
But a second more powerful step involves a gitattribute filter driverand add a smudge step
但是第二个更强大的步骤涉及一个gitattribute 过滤器驱动程序并添加一个涂抹步骤
Whenever you would update your working tree, a script could, only for the files you have specified in the .gitattributes
, force the LF eol
and any other formatting option you want to enforce.
If the "clear
" script doesn't do anything, you will have (after commit) transformed your files, applying exactly the format you need them to follow.
每当您更新工作树时,脚本都可以(仅针对您在 中指定的文件).gitattributes
强制LF eol
和您想要强制执行的任何其他格式选项。
如果“ clear
”脚本没有做任何事情,您将(在提交后)转换您的文件,完全应用您需要它们遵循的格式。
回答by Chronial
The proper way to get LF endings in Windows is to first set core.autocrlf
to false
:
在 Windows 中获得 LF 结尾的正确方法是首先设置core.autocrlf
为false
:
git config --global core.autocrlf false
You need to do this if you are using msysgit, because it sets it to true
in its system settings.
如果您使用的是 msysgit,则需要这样做,因为它true
在其系统设置中将其设置为。
Now git won't do any line ending normalization. If you want files you check in to be normalized, do this: Set text=auto
in your .gitattributes
for all files:
现在 git 不会做任何行结束规范化。如果您希望对您签入的文件进行规范化,请执行以下操作:text=auto
在您.gitattributes
的所有文件中设置:
* text=auto
And set core.eol
to lf
:
并设置core.eol
为lf
:
git config --global core.eol lf
Now you can also switch single repos to crlf (in the working directory!) by running
现在您还可以通过运行将单个存储库切换到 crlf(在工作目录中!)
git config core.eol crlf
After you have done the configuration, you might want git to normalize all the files in the repo. To do this, go to to the root of your repo and run these commands:
完成配置后,您可能希望 git 规范化repo中的所有文件。为此,请转到存储库的根目录并运行以下命令:
git rm --cached -rf .
git diff --cached --name-only -z | xargs -n 50 -0 git add -f
If you now want git to also normalize the files in your working directory, run these commands:
如果您现在希望 git 也规范化您工作目录中的文件,请运行以下命令:
git ls-files -z | xargs -0 rm
git checkout .
回答by Ben Liyanage
I come back to this answer fairly often, though none of these are quite right for me. That said, the right answer for me is a mixture of the others.
我经常回到这个答案,尽管这些都不适合我。也就是说,对我来说正确的答案是其他答案的混合。
What I find works is the following:
我发现的作品如下:
git config --global core.eol lf
git config --global core.autocrlf input
For repos that were checked out after those global settings were set, everything will be checked out as whatever it is in the repo – hopefully LF
(\n
). Any CRLF
will be converted to just LF
on checkin.
对于在设置这些全局设置后签出的存储库,所有内容都将按照存储库中的任何内容进行检出 - 希望是LF
( \n
)。AnyCRLF
将转换为仅LF
在签入时。
With an existing repo that you have already checked out – that has the correct line endings in the repo but not your working copy – you can run the following commands to fix it:
使用您已经签出的现有存储库 - 在存储库中具有正确的行结尾但不是您的工作副本 - 您可以运行以下命令来修复它:
git rm -rf --cached .
git reset --hard HEAD
This will delete (rm
) recursively (r
) without prompt (-f
), all files except those that you have edited (--cached
), from the current directory (.
). The reset
then returns all of those files to a state where they have their true line endings (matching what's in the repo).
这将在没有提示rm
( r
) 的情况下递归 ( )删除 ( )当前目录 ( ) 中-f
除您已编辑 ( --cached
)之外的所有文件.
。在reset
随后返回所有这些文件到他们自己的真实行结束(匹配什么在回购)的状态。
If you need to fix the line endings of files in a repo, I recommend grabbing an editor that will let you do that in bulk like IntelliJ or Sublime Text, but I'm sure any good one will likely support this.
如果您需要修复 repo 中文件的行尾,我建议使用一个编辑器,让您可以像 IntelliJ 或 Sublime Text 那样批量执行此操作,但我相信任何好的编辑器都可能支持这一点。
回答by koppor
Context
语境
If you
如果你
- want to force all users to have LF line endings for text files and
- you cannot ensure that all users change their git config,
- 想要强制所有用户为文本文件使用 LF 行尾,并且
- 您无法确保所有用户都更改了他们的 git 配置,
you can do that starting with git 2.10. 2.10 or later is required, because 2.10 fixed the behavior of text=auto together with eol=lf. Source.
你可以从 git 2.10 开始这样做。需要 2.10 或更高版本,因为2.10 修复了 text=auto 和 eol=lf 的行为。来源。
Solution
解决方案
Put a .gitattributes
file in the root of your git repository having following contents:
将一个.gitattributes
文件放在 git 存储库的根目录中,其内容如下:
* text=auto eol=lf
Commit it.
承诺吧。
Optional tweaks
可选的调整
You can also add an .editorconfig
in the root of your repository to ensure that modern tooling creates new files with the desired line endings.
您还可以.editorconfig
在存储库的根目录中添加,以确保现代工具创建具有所需行结尾的新文件。
# EditorConfig is awesome: http://EditorConfig.org
# top-most EditorConfig file
root = true
# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
回答by kusma
core.autocrlf=input
is the right setting for what you want, but you might have to do a git update-index --refresh
and/or a git reset --hard
for the change to take effect.
core.autocrlf=input
是您想要的正确设置,但您可能必须执行 agit update-index --refresh
和/或 agit reset --hard
才能使更改生效。
With core.autocrlf
set to input
, git will not apply newline-conversion on check-out (so if you have LF in the repo, you'll get LF), but it will make sure that in case you mess up and introduce some CRLFs in the working copy somehow, they won't make their way into the repo.
随着core.autocrlf
设置为input
,git会不退房(所以如果你在回购有LF,你会得到LF)申请换行转换,但将确保万一你搞砸了,介绍的工作的一些CRLFs以某种方式复制,他们不会进入回购。
回答by c-santana
You can find the solution to this problem at: https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings
您可以在以下位置找到此问题的解决方案:https: //help.github.com/en/github/using-git/configuring-git-to-handle-line-endings
Simplified description of how you can solve this problem on windows:
如何在 Windows 上解决此问题的简化说明:
Global settings for line endings The git config core.autocrlf command is used to change how Git handles line endings. It takes a single argument.
行尾的全局设置 git config core.autocrlf 命令用于更改 Git 处理行尾的方式。它需要一个参数。
On Windows, you simply pass true to the configuration. For example: C:>git config --global core.autocrlf true
在 Windows 上,您只需将 true 传递给配置。例如:C:>git config --global core.autocrlf true
Good luck, I hope I helped.
祝你好运,我希望我有所帮助。