有没有办法确定现有 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:17:37  来源:igfitidea点击:

Is there a way to determine the line endings in a existing git repo?

gitcore.autocrlf

提问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.autocrlfsetting), try the following:

要检查存储库中实际提交的行尾(无论您的core.autocrlf设置如何),请尝试以下操作:

git grep -I --files-with-matches --perl-regexp '\r' HEAD

(-Imeans 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 eolgitattributes directivefor a more fine-grained control.

正如我在“使用您提到的代码分发 git 配置”中所解释的那样,我仍然会将该设置 ( core.autocrlf)保留false为,并使用gitattributes 指令进行更细粒度的控制。eol

That being said, to detect a mixed line endings:

话虽如此,要检测混合行结尾:

  • set core.autocrlfto true
  • git cloneyour repo
  • git diff: if diffs are visible just after your clone... some automatic eol conversions just took place in the working tree.
  • 设置core.autocrlftrue
  • 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 存储库中的行结尾:

  1. Set core.autocrlfto false, so it will not change file endings while transmitting files.
  2. git cloneyour repo for ex. in a new directory.
  3. 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.
  1. 设置core.autocrlffalse,因此在传输文件时不会更改文件结尾。
  2. git clone你的前回购。在一个新目录中。
  3. 使用显示行尾的文本编辑器打开文件(例如 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\configfile, and change the value from falseto true

如果你想改变这个,编辑C:\ProgramData\Git\config文件,并将值从 更改falsetrue

Note: This only applies for Windows operating systems.

注意:这仅适用于 Windows 操作系统。