本文翻译自:How to change line-ending settings
Is there a file or menu that will let me change the settings on how to deal with line endings? 是否有文件或菜单可以让我更改如何处理行尾的设置?
There are 3 options: 有3个选项:
Checkout Windows-style, commit Unix-style 签出Windows风格,提交Unix风格
Git will convert LF to CRLF when checking out text files. 签出文本文件时,Git会将LF转换为CRLF。 When committing text files, CRLF will be converted to LF. 提交文本文件时,CRLF将转换为LF。 For cross-platform projects, this is the recommended setting on Windows ("core.autocrlf" is set to "true") 对于跨平台项目,这是Windows上的推荐设置(“ core.autocrlf”设置为“ true”)
Checkout as-is, commit Unix-style 按原样签出,以Unix样式提交
Git will not perform any conversion when checking out text files. 签出文本文件时,Git不会执行任何转换。 When committing text files, CRLF will be converted to LF. 提交文本文件时,CRLF将转换为LF。 For cross-platform projects this is the recommended setting on Unix ("core.autocrlf" is set to "input"). 对于跨平台项目,这是在Unix上的推荐设置(“ core.autocrlf”设置为“ input”)。
Checkout as-is, commit as-is 按原样签出,按原样提交
Git will not perform any conversions when checking out or committing text files. 当签出或提交文本文件时,Git不会执行任何转换。 Choosing this option is not recommended for cross-platform projects ("core.autocrlf" is set to "false") 不建议跨平台项目选择此选项(“ core.autocrlf”设置为“ false”)
#1楼
参考:https://stackoom.com/question/hiRz/如何更改行尾设置
#2楼
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 Git to the section named "core.autocrlf" 有关详细信息,请在此链接中向下滚动到Pro Git到名为“ core.autocrlf”的部分。
If you want to know what file this 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 global config文件应该在文本编辑器中打开,您可以看到该文件的加载位置。
#3楼
For a repository setting solution, that can be redistributed to all developers, check out the text attribute in the .gitattributes file. 对于可以重新分发给所有开发人员的存储库设置解决方案,请查看.gitattributes文件中的text属性。 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. 这样,开发人员不必在存储库上手动设置自己的行尾,而且由于不同的存储库可以具有不同的行尾样式,因此至少在我看来,global 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. -文本]将强制git在签入和签出时不接触行尾。 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. 同样,如果您作为开发人员仍希望在签入时进行行尾转换,则仍可以设置路径以匹配某些文件或在存储库中设置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文件和text属性: 使用Git最好的CRLF(回车,换行)处理策略是什么?
#4楼
If you want to convert back the file formats which have been changed to UNIX Format from PC format. 如果要转换回已从PC格式更改为UNIX格式的文件格式。
(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)您需要重新安装乌龟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
#5楼
Line ending format used in OS OS中使用的行尾格式
- Windows:
CR
(Carriage Return\\r
) andLF
(LineFeed\\n
) pair Windows:CR
(回车\\r
)和LF
(LineFeed\\n
)对 - OSX,Linux:
LF
(LineFeed\\n
) OSX,Linux:LF
(LineFeed\\n
)
We can configure git to auto-correct line ending formats for each OS in two ways. 我们可以配置git以两种方式为每个操作系统自动更正行尾格式。
- Git Global configuration Git全局配置
- Use
.gitattributes
file 使用.gitattributes
文件
Global Configuration 全局配置
In Linux/OSX 在Linux / OSX中git config --global core.autocrlf true
This will fix any CRLF
to LF
when you commit. 提交时,这会将所有CRLF
固定为LF
。
* text=auto
This will make sure when you checkout in windows, all LF
will convert to CRLF
这样可以确保当您在Windows中结帐时,所有LF
都将转换为CRLF
.gitattributes File .gitattributes文件
It is a good idea to keep a .gitattributes
file as we don't want to expect everyone in our team set their config. 保留.gitattributes
文件是一个好主意,因为我们不希望团队中的每个人都设置他们的配置。 This file should keep in repo's root path and if exist one, git will respect it. 该文件应保留在repo的根路径中,如果存在,则git将尊重它。
*.jpg binary
This will treat all files as text files and convert to OS's line ending on checkout and back to LF
on commit automatically. 这会将所有文件视为文本文件,并转换为OS的行(在结帐时结束),并在提交后自动返回到LF
。 If wanted to tell explicitly, then use 如果要明确告诉,则使用
* text eol=crlf * text eol=lf
First one is for checkout and second one is for commit. 第一个用于签出,第二个用于提交。
*.jpg binary
Treat all .jpg
images as binary files. 将所有.jpg
图像视为二进制文件。 So no conversion needed 因此无需转换
#6楼
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(fyi .git是一个隐藏文件夹)中的文件
[auto]
crlf = false
at the end of the file. 在文件末尾。 I suppose changing the file does the same trick as well. 我想更改文件也能达到同样的效果。