有没有办法确定现有 git repo 中的行尾?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11824428/
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
Is there a way to determine the line endings in a existing git repo?
提问by U. N. Owen
Is there a way to determine the line endings in a existing git repository?
有没有办法确定现有 git 存储库中的行尾?
If I clone a existing repository how do I determine what core.autocrlf was used by the creator?
如果我克隆了一个现有的存储库,我如何确定创建者使用了什么 core.autocrlf?
I'm still uncertain whats the best setting for core.autocrlf e.g on a windows box (since there are multiple opinions: Distributing git configuration with the codeor https://help.github.com/articles/dealing-with-line-endings)
我仍然不确定 core.autocrlf 的最佳设置是什么,例如在 Windows 盒子上(因为有多种意见: 使用代码或https://help.github.com/articles/dealing-with-line-分发 git 配置-结局)
Bonus question:Can you determine on windows (with standard tools) if a repo has mixed line endings (by wrong core.autocrlf setting) through all commits?
额外问题:您能否在 Windows 上(使用标准工具)确定一个 repo 在所有提交中是否具有混合行尾(通过错误的 core.autocrlf 设置)?
回答by robinst
To check what line endings were actually committed in the repository (regardless of your core.autocrlf
setting), try the following:
要检查存储库中实际提交的行尾(无论您的core.autocrlf
设置如何),请尝试以下操作:
git grep -I --files-with-matches --perl-regexp '\r' HEAD
(-I
means that binary files should not be looked at.)
(-I
意味着不应查看二进制文件。)
回答by VonC
I would still maintain that setting (core.autocrlf
) to false
, as I explain in "Distributing git configuration with the code" that you mention, and uses eol
gitattributes directivefor a more fine-grained control.
正如我在“使用您提到的代码分发 git 配置”中所解释的那样,我仍然会将该设置 ( core.autocrlf
)保留false
为,并使用gitattributes 指令进行更细粒度的控制。eol
That being said, to detect a mixed line endings:
话虽如此,要检测混合行结尾:
- set
core.autocrlf
totrue
git clone
your repogit diff
: if diffs are visible just after your clone... some automatic eol conversions just took place in the working tree.
- 设置
core.autocrlf
为true
git clone
你的回购git diff
:如果在您的克隆之后可以看到差异……一些自动 eol 转换刚刚在工作树中发生。
Update 2016 (4 years later): a more modern way to detect eol changes:
2016 年更新(4 年后):一种更现代的检测 eol 变化的方法:
git -c color.diff.whitespace="red reverse" diff -R -- afile
回答by Dr. Botwing
The best line endings practice is here: https://stackoverflow.com/a/10855862
最好的行尾做法在这里:https: //stackoverflow.com/a/10855862
To determine the line endings in a existing git repository:
要确定现有 git 存储库中的行结尾:
- Set
core.autocrlf
tofalse
, so it will not change file endings while transmitting files. git clone
your repo for ex. in a new directory.- Use the text editor that shows line endings to open the files (for ex. PHPStorm does). You should open several files as line endings may differ from file to file.
- 设置
core.autocrlf
为false
,因此在传输文件时不会更改文件结尾。 git clone
你的前回购。在一个新目录中。- 使用显示行尾的文本编辑器打开文件(例如 PHPStorm)。您应该打开多个文件,因为行尾可能因文件而异。
You can't determine what core.autocrlf was used by the creator as it is local config except the repo has .gitattributes file.
您无法确定创建者使用了什么 core.autocrlf,因为它是本地配置,除非 repo 具有 .gitattributes 文件。
On Windows if you are not using .gitattributes just use core.autocrlf true as it set by default.
在 Windows 上,如果您不使用 .gitattributes,只需使用默认设置的 core.autocrlf true。
回答by Aniket A. Aryamane
In Windows, just run the below command from the command prompt:
在 Windows 中,只需从命令提示符运行以下命令:
git config --list
This will list all the git configuration variables.
这将列出所有 git 配置变量。
If you want to get the individual config setting (e.g. for core.autocrlf), run the following command on the windows command prompt:
如果您想获取单独的配置设置(例如 core.autocrlf),请在 Windows 命令提示符下运行以下命令:
git config --get core.autocrlf
This will either give you a "true
" OR "false
" value.
这将给您一个“ true
”或“ false
”值。
IF you wish to change this, edit C:\ProgramData\Git\config
file, and change the value from false
to true
如果你想改变这个,编辑C:\ProgramData\Git\config
文件,并将值从 更改false
为true
Note: This only applies for Windows operating systems.
注意:这仅适用于 Windows 操作系统。