简体   繁体   English

处理行以GIT结尾

[英]Handeling line ending in GIT

Let's say we have team of 10 developers. 假设我们有10个开发人员团队。 Half of them use Windows and half Linux. 他们一半使用Windows,一半使用Linux。 All of them work together on a project, sharing work using GIT. 他们都在一个项目上一起工作,使用GIT共享工作。 Problem with CRLF/LF begins ... CRLF / LF问题开始...

Exmaple: One developer with Window works on the same thing together with developer which is using Linux. 示例:一名使用Window的开发人员与使用Linux的开发人员一起从事同一工作。 Both of them need to view/edit the same files. 他们两个都需要查看/编辑相同的文件。

Will there be problem with CRLF and LF ? CRLF和LF会有问题吗? What are the ways GIT would handle this problem ? GIT将如何处理此问题? (force Linux isn't solution) (强制Linux不是解决方案)

You can see more with the recent article " Git for Windows: Line Endings " from Edward Thomson (Ex-GitHubber, now Microsoft, now... errr GitHubber again) 您可以在Edward Thomson的最新文章“ Windows的Git:行尾 ”中看到更多内容(前GitHubber,现在是Microsoft,现在又是errr GitHubber)

The key to dealing with line endings is to make sure your configuration is committed to the repository, using .gitattributes . 处理行尾的关键是使用.gitattributes确保配置已提交到存储库 For most people, this is as simple as creating a file named .gitattributes at the root of your repository that contains one line: 对于大多数人来说,这.gitattributes在存储库的根目录中创建一个名为.gitattributes的文件一样简单,该文件包含一行:

* text=auto

With this set, Windows users will have text files converted from Windows style line endings (\\r\\n) to Unix style line endings (\\n) when they're added to the repository. 使用此设置,将Windows用户添加到存储库后,会将文本文件从Windows样式行结尾(\\ r \\ n)转换为Unix样式行结尾(\\ n)。

Why not core.autocrlf ? 为什么不选择core.autocrlf

Originally, Git for Windows introduced a different approach for line endings that you may have seen: core.autocrlf . 最初,用于Windows的Git引入了另一种行尾处理方法: core.autocrlf This is a similar approach to the attributes mechanism: the idea is that a Windows user will set a Git configuration option core.autocrlf=true and their line endings will be converted to Unix style line endings when they add files to the repository. 这是一种类似于属性机制的方法:想法是Windows用户将设置一个Git配置选项core.autocrlf=true并且当他们向存储库添加文件时,它们的行尾将转换为Unix样式的行尾。

The difference between these two options is subtle, but critical: the .gitattributes is set in the repository, so its shared with everybody. 这两个选项之间的区别是微妙的,但很关键: .gitattributes是在存储库中设置的,因此它与所有人共享。 But core.autocrlf is set in the local Git configuration. 但是core.autocrlf是在本地Git配置中设置的。 That means that everybody has to remember to set it, and set it identically. 这意味着每个人都必须记住要进行设置,并进行相同的设置。

I would add that core.autocrlf applies to all files including binaries ones, whereas .gitattribure core.eol directive can be set for specific files (like *.cpp for instance) 我要补充一点core.autocrlf适用于所有文件,包括二进制文件,而.gitattribure core.eol指令可以为特定文件设置(例如*.cpp

I have always recommended that .gitattributes approach . 一直建议.gitattributes方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM