处理以 GIT 结尾的行

Handeling line ending in GIT

假设我们有 10 名开发人员。其中一半使用 Windows,另一半使用 Linux。他们所有人都在一个项目上合作,使用 GIT 共享工作。 CRLF/LF 的问题开始...

示例:一位 Window 的开发人员与 Linux 的开发人员一起处理同一件事。他们都需要 view/edit 相同的文件。

CRLF 和 LF 会不会有问题? GIT 有什么方法可以解决这个问题? (强制 Linux 不是解决方案)

您可以通过最近的文章“Git for Windows: Line Endings" from Edward Thomson(前 GitHubber,现在是 Microsoft,现在...又是 GitHubber)

了解更多信息

The key to dealing with line endings is to make sure your configuration is committed to the repository, using .gitattributes. For most people, this is as simple as creating a file named .gitattributes at the root of your repository that contains one line:

* 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.

Why not core.autocrlf?

Originally, Git for Windows introduced a different approach for line endings that you may have seen: 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.

The difference between these two options is subtle, but critical: the .gitattributes is set in the repository, so its shared with everybody. But core.autocrlf is set in the local Git configuration. That means that everybody has to remember to set it, and set it identically.

我要补充一点,core.autocrlf 适用于 所有 文件,包括二进制文件,而 .gitattribure core.eol 指令可以为特定文件设置(例如 *.cpp

我有 always recommended that .gitattributes approach.