git 如何更改行尾设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10418975/
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-10 13:36:03  来源:igfitidea点击:

How to change line-ending settings

gitmsysgit

提问by qwertymk

Is there a file or menu that will let me change the settings on how to deal with line endings?

是否有文件或菜单可以让我更改有关如何处理行尾的设置?

I read there are 3 options:

我读到有 3 个选项:

  1. Checkout Windows-style, commit Unix-style

    Git will convert LF to CRLF when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects, this is the recommended setting on Windows ("core.autocrlf" is set to "true")

  2. Checkout as-is, commit Unix-style

    Git will not perform any conversion when checking out text files. When committing text files, CRLF will be converted to LF. For cross-platform projects this is the recommended setting on Unix ("core.autocrlf" is set to "input").

  3. Checkout as-is, commit as-is

    Git will not perform any conversions when checking out or committing text files. Choosing this option is not recommended for cross-platform projects ("core.autocrlf" is set to "false")

  1. 签出 Windows 风格,提交 Unix 风格

    Git 会在检出文本文件时将 LF 转换为 CRLF。提交文本文件时,CRLF 将转换为 LF。对于跨平台项目,这是 Windows 上的推荐设置(“core.autocrlf”设置为“true”)

  2. 按原样结帐,提交 Unix 风格

    Git 在检出文本文件时不会执行任何转换。提交文本文件时,CRLF 将转换为 LF。对于跨平台项目,这是 Unix 上的推荐设置(“core.autocrlf”设置为“input”)。

  3. 按原样结帐,按原样提交

    Git 在检出或提交文本文件时不会执行任何转换。不建议跨平台项目选择此选项(“core.autocrlf”设置为“false”)

采纳答案by CodingWithSpike

The normal way to control this is with git config

控制这种情况的正常方法是 git config

For example

例如

git config --global core.autocrlf true

For details, scroll down in this link to Pro Gitto the section named "core.autocrlf"

有关详细信息,请在此链接中向下滚动Pro Git到名为“core.autocrlf”的部分



If you want to know what filethis is saved in, you can run the command:

如果你想知道这是保存在哪个文件中,可以运行以下命令:

git config --global --edit

and the git global config file should open in a text editor, and you can see where that file was loaded from.

并且 git 全局配置文件应该在文本编辑器中打开,您可以看到该文件是从哪里加载的。

回答by Jasnan

Line ending format used in OS

操作系统中使用的行尾格式

  • Windows: CR(Carriage Return \r) and LF(LineFeed \n) pair
  • OSX,Linux: LF(LineFeed \n)
  • Windows:(CR回车\r)和LF(换行\n)对
  • OSX,Linux:(LF换行\n

We can configure git to auto-correct line ending formats for each OS in two ways.

我们可以通过两种方式将 git 配置为自动更正每个操作系统的行尾格式。

  1. Git Global configuration
  2. Use .gitattributesfile
  1. Git全局配置
  2. 使用.gitattributes文件

Global Configuration

全局配置

在 Linux/OSX 中
git config --global core.autocrlf input

This will fix any CRLFto LFwhen you commit.

这将解决任何CRLFLF当你提交。

在 Windows 中
git config --global core.autocrlf true

This will make sure when you checkout in windows, all LFwill convert to CRLF

这将确保当您在 Windows 中结帐时,所有内容都LF将转换为CRLF

.gitattributes File

.gitattributes 文件

It is a good idea to keep a .gitattributesfile as we don't want to expect everyone in our team set their config. This file should keep in repo's root path and if exist one, git will respect it.

保留.gitattributes文件是个好主意,因为我们不希望团队中的每个人都设置他们的配置。这个文件应该保留在 repo 的根路径中,如果存在,git 会尊重它。

* text=auto

This will treat all files as text files and convert to OS's line ending on checkout and back to LFon commit automatically. If wanted to tell explicitly, then use

这会将所有文件视为文本文件,并自动转换为操作系统的行结束于结帐并返回到LF提交。如果想明确告诉,然后使用

* text eol=crlf
* text eol=lf

First one is for checkout and second one is for commit.

第一个用于结帐,第二个用于提交。

*.jpg binary

Treat all .jpgimages as binary files, regardless of path. So no conversion needed.

将所有.jpg图像视为二进制文件,无论路径如何。所以不需要转换。

Or you can add path qualifiers:

或者您可以添加路径限定符:

my_path/**/*.jpg binary

回答by Fazi

For a repository setting solution, that can be redistributed to all developers, check out the textattribute in the .gitattributesfile. This way, developers dont have to manually set their own line endings on the repository, and because different repositories can have different line ending styles, global core.autocrlf is not the best, at least in my opinion.

对于可以重新分发给所有开发人员的存储库设置解决方案,请查看.gitattributes文件中的text属性。这样,开发人员就不必在仓库上手动设置自己的行尾,而且由于不同的仓库可以有不同的行尾样式,全局 core.autocrlf 并不是最好的,至少在我看来是这样。

For example unsetting this attribute on a given path [.- text] will force git not to touch line endings when checking in and checking out. In my opinion, this is the best behavior, as most modern text editors can handle both type of line endings. Also, if you as a developer still want to do line ending conversion when checking in, you can still set the path to match certain files or set the eol attribute (in .gitattributes) on your repository.

例如,在给定路径 [ 上取消设置此属性- text] 将强制 git 在签入和签出时不要触摸行尾。在我看来,这是最好的行为,因为大多数现代文本编辑器都可以处理这两种类型的行尾。此外,如果您作为开发人员仍然希望在签入时进行行尾转换,您仍然可以设置路径以匹配某些文件或在您的存储库中设置 eol 属性(在 .gitattributes 中)。

Also check out this related post, which describes .gitattributes file and text attribute in more detail: What's the best CRLF (carriage return, line feed) handling strategy with Git?

另请查看这篇相关文章,其中更详细地描述了 .gitattributes 文件和文本属性:Git 的最佳 CRLF(回车、换行)处理策略是什么?

回答by Travel and code enthousiast

For me what did the trick was running the command

对我来说,运行命令的诀窍是什么

git config auto.crlf false

inside the folder of the project, I wanted it specifically for one project.

在项目的文件夹内,我想要它专门用于一个项目。

That command changed the file in path {project_name}/.git/config (fyi .git is a hidden folder) by adding the lines

该命令通过添加行更改了路径 {project_name}/.git/config (仅供参考 .git 是一个隐藏文件夹)中的文件

[auto]
    crlf = false

at the end of the file. I suppose changing the file does the same trick as well.

在文件的末尾。我想更改文件也有同样的技巧。

回答by Nishanth

If you want to convert back the file formats which have been changed to UNIX Format from PC format.

如果要将已更改为 UNIX 格式的文件格式从 PC 格式转换回。

(1)You need to reinstall tortoise GIT and in the "Line Ending Conversion" Section make sure that you have selected "Check out as is - Check in as is"option.

(1)您需要重新安装tortoise GIT,并在“行尾转换”部分确保您选择了“按原样签出-按原样签入”选项。

(2)and keep the remaining configurations as it is.

(2) 并保持其余配置不变。

(3)once installation is done

(3)一旦安装完成

(4)write all the file extensions which are converted to UNIX format into a text file (extensions.txt).

(4)将所有转换为UNIX格式的文件扩展名写入一个文本文件(extensions.txt)。

ex:*.dsp
   *.dsw

(5) copy the file into your clone Run the following command in GITBASH

(5) 将文件复制到你的克隆中 在 GITBASH 中运行以下命令

while read -r a;
do
find . -type f -name "$a" -exec dos2unix {} \;
done<extension.txt