禁用 git EOL 转换

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

Disable git EOL Conversions

git

提问by imallett

I am trying to get git to not change any line endings whatsoever for any operation. Unfortunately, it seems to do so not matter what. I have reduced it down to the following test case, which has as many different mechanisms for disabling this behavior as I could find.

我试图让 git 不更改任何操作的任何行尾。不幸的是,这样做似乎无所谓。我已将其简化为以下测试用例,其中包含尽可能多的不同机制来禁用此行为。



  • Begin with two machines (Windows computer = A, Linux computer = B)
  • On both machines: git config --global core.autocrlf false
  • On both machines: git config --global core.eol crlf(just in case)
  • 从两台机器开始(Windows 计算机 = A,Linux 计算机 = B)
  • 在两台机器上: git config --global core.autocrlf false
  • 在两台机器上:(git config --global core.eol crlf以防万一)


  • Make new repository on A. From an empty folder:
    • git init --shared(then unhide the created .gitdirectory)
    • Make a new file .gitignorein the repository
    • Make a new file .gitattributesin the repository with the single line: * -text
    • git add ., then git commit -m "initial commit"to work around, e.g. this.
    • git branch master_recv
    • Add remotes
  • Make a new file document.txtin the repository containing CRLF
  • Commit: git add -A, then git commit -m "<something>"
  • Note that A's document.txtstill contains CRLF (and deleting it and resetting with --hardreturns the version still with CRLF)
  • 在 A 上创建新存储库。从一个空文件夹:
    • git init --shared(然后取消隐藏创建的.git目录)
    • .gitignore在存储库中创建一个新文件
    • .gitattributes使用单行在存储库中创建一个新文件:* -text
    • git add .,然后git commit -m "initial commit"解决,例如this
    • git branch master_recv
    • 添加遥控器
  • document.txt在包含 CRLF 的存储库中创建一个新文件
  • 提交:git add -A,然后git commit -m "<something>"
  • 请注意,Adocument.txt仍然包含 CRLF(删除它并使用重置--hard返回仍然带有 CRLF 的版本)


  • SCP the whole directory to computer B
  • Add a new file new filecontaining CRLF
  • Commit: git add -A, then git commit -m "<something>"
  • Note that B's document.txtand B's new fileboth still contain CRLF
  • SCP整个目录到电脑B
  • 添加一个new file包含 CRLF的新文件
  • 提交:git add -A,然后git commit -m "<something>"
  • 请注意,Bdocument.txt和 Bnew file仍然包含 CRLF


  • Pull B's master to A: git pull <remote> master:master_recv
  • A's document.txthas changed to LF. The added file new filealso contains LF.
  • 将 B 的主人拉到 A: git pull <remote> master:master_recv
  • Adocument.txt已更改为 LF。添加的文件new file还包含 LF。

The problem does not occur if B is a Windows machine.

如果 B 是 Windows 计算机,则不会出现此问题。

采纳答案by imallett

I figured it out. It seems that the SCP program was converting the line endings. I noticed this when I tried deliberately making a file with LF endings and then observing that it appeared as CRLF when downloaded.

我想到了。似乎 SCP 程序正在转换行尾。当我故意制作一个以 LF 结尾的文件,然后观察到它在下载时显示为 CRLF 时,我注意到了这一点。

Since this was the solution for me, I'm accepting this answer, but people of the future should also refer to the other answers for a more general solution.

由于这是我的解决方案,我接受这个答案,但未来的人们也应该参考其他答案以获得更通用的解决方案。

回答by Gene

Inside your project, there should be a .gitattributesfile. Most of the time, it should look like below (or this screen-shot):

在您的项目中,应该有一个.gitattributes文件。大多数情况下,它应该如下所示(或此屏幕截图):

# Handle line endings automatically for files detected as text 
# and leave all files detected as binary untouched.
* text=auto

# Never modify line endings of our bash scripts
*.sh -crlf

#
# The above will handle all files NOT found below
#
# These files are text and should be normalized (Convert crlf => lf)
*.css           text
*.html          text
*.java          text
*.js            text
*.json          text
*.properties    text
*.txt           text
*.xml           text

# These files are binary and should be left untouched
# (binary is macro for -text -diff)
*.class         binary
*.jar           binary
*.gif           binary
*.jpg           binary
*.png           binary

Change * text=autoto * text=falseto disable automatic handling (see screen-shot).

更改* text=auto* text=false禁用自动处理(请参阅屏幕截图)。

Like this:

像这样:

enter image description here

在此处输入图片说明

If your project doesn't have a .gitattributes file, then the line endings are set by your git configurations. To change your git configurations, do this:

如果您的项目没有 .gitattributes 文件,则行尾由您的 git 配置设置。要更改您的 git 配置,请执行以下操作:

Go to the config file in this directory:

转到此目录中的配置文件:

1) C:\ProgramData\Git\config

1) C:\ProgramData\Git\config

2) Open up the config file in Notepad++ (or whatever text editor you prefer)

2)在 Notepad++(或任何你喜欢的文本编辑器)中打开配置文件

3) Change "autocrlf=" to false.

3) 将“autocrlf=”更改为false。

enter image description here

在此处输入图片说明

回答by VonC

One simple solution is:

一种简单的解决方案是:

  • make sure core.autocrlf is set to false for allrepos:
    git config --global core.autocrlf false
  • re-clone your repo, and check no EOL conversion is done.
  • or, since Git 2.16 (Q1 2018), keep your current repo, and do a git add --renormalize .
  • 确保所有存储库的core.autocrlf 都设置为 false :
    git config --global core.autocrlf false
  • 重新克隆您的 repo,并检查没有完成 EOL 转换。
  • 或者,自 Git 2.16 (Q1 2018) 起,保留当前的 ​​repo,并执行git add --renormalize .

If there are conversions automatically done, that mean a .gitattributescore.eoldirectiveis there within the repo.

如果有自动完成的转换,那意味着在 repo 中有一个.gitattributescore.eol指令

With Git 2.8+ (March 2016), check if there are still eol transformation with:

使用Git 2.8+ (March 2016),检查是否还有 eol 转换:

git ls-files --eol

回答by Lazy Badger

From gitattributes(5) Manual Page"Effects" topic

来自gitattributes(5) 手册页“效果”主题

text

This attribute enables and controls end-of-line normalization. When a text file is normalized, its line endings are converted to LF in the repository. To control what line ending style is used in the working directory, use the eolattribute for a single file and the core.eolconfiguration variable for all text files.

Set

Setting the text attribute on a path enables end-of-line normalization and marks the path as a text file. End-of-line conversion takes place without guessing the content type.

UnsetUnsetting the text attribute on a path tells Git not to attempt any end-of-line conversion upon checkin or checkout.

text

此属性启用并控制行尾规范化。对文本文件进行规范化后,其行尾将在存储库中转换为 LF。要控制在工作目录中使用的行尾样式,请eol对单个文件使用属性,core.eol对所有文本文件使用配置变量。

Set

在路径上设置 text 属性可启用行尾规范化并将路径标记为文本文件。行尾转换无需猜测内容类型即可进行。

Unset取消设置路径上的 text 属性会告诉 Git 在签入或签出时不要尝试任何行尾转换。

core.autocrlfin new (1.7.2+) Git not used, core.eoland correct setting|unsetting of text-attribute considered as more reliable way

core.autocrlf在新的 (1.7.2+) Git 中未使用,core.eol正确设置|取消设置文本属性被认为是更可靠的方法