在git repo和工作副本中强制LF eol

我有一个github上托pipe的git存储库。 许多文件最初是在Windows上开发的,对于行结尾我并不太在意。 当我执行初始提交时,我也没有任何gitconfiguration来执行正确的行尾。 结果是,我有一些CRLF行结束在我的github库中的文件。

我现在正在Linux上部分开发,我想清理线路结束。 我怎样才能确保文件在github上使用LF正确存储,并在我的工作副本中有LF?

我已经build立了一个包含text eol=LF.gitattributes文件。 那是对的吗? 有了这个承诺和推动,我可以只是rm我的本地回购,并从github重新克隆,以获得预期的效果?

如果没有关于存储库中的文件(纯源代码,图像,可执行文件…)的信息,有点难以回答这个问题:)

除此之外,我会考虑你愿意在你的工作目录中默认使用LF作为行结尾,因为你愿意确保文本文件的LF行结束符在你使用Windows或Linux的.git存储库中。 确实比对不起更安全。

但是,还有一个更好的select:从Linux工作目录的LF行结束中获益,在工作目录中的CRLF行结束符和存储库中的LF行结束符。

由于您部分在Linux和Windows上工作,请确保将core.eol设置为native并将core.autocrlf设置为true

然后,用下面的内容replace.gitattributes文件的内容

 * text=auto 

这将让Git处理您的提交和结账时的自动化行结束转换。 二进制文件不会被改变,被检测为文本文件的文件会看到在运行中转换的行结束符。

然而,如你知道你的仓库的内容,你可能会给Git帮助他从二进制文件中检测文本文件。

如果您在基于C的image processing项目上工作,请使用以下内容replace.gitattributes文件的内容

 * text=auto *.txt text *.c text *.h text *.jpg binary 

这将确保文件扩展名为c,h或txt的文件将在您的仓库中以LF行尾存储,并且在工作目录中将具有本地行结尾。 Jpeg文件不会被触及。 所有其他人将从上面看到的相同的自动过滤中受益。

为了更深入地了解所有这些内在细节,我build议你从Githubber的Tim Clem跳进这个非常好的post: “注意你的路线的末端”

作为一个真实世界的例子,你也可以看看这个提交.gitattributes文件的变化。

更新到答案考虑以下评论

我其实不想在我的Windows目录中使用CRLF,因为我的Linux环境实际上是共享Windows目录的VirtualBox

说得通。 感谢您的澄清。 在这个特定的上下文中, .gitattributes文件本身是不够的。

针对您的存储库运行以下命令

 $ git config core.eol lf $ git config core.autocrlf input 

由于您的存储库是在您的Linux和Windows环境之间共享的,因此会更新这两个环境的本地configuration文件。 core.eol将确保文本文件在结帐时带有LF行结尾。 core.autocrlf将确保文本文件中的潜在 CRLF(例如由复制/粘贴操作产生)将在您的存储库中转换为LF。

也可以通过创build一个.gitattributes文件来帮助Git区分什么文本文件,其中包含类似于以下内容的文件:

 # Autodetect text files * text=auto # ...Unless the name matches the following # overriding patterns # Definitively text files *.txt text *.c text *.h text # Ensure those won't be messed up with *.jpg binary *.data binary 

如果您决定创build.gitattributes文件,请提交

最后,确保git status提到“没有提交(工作目录干净)” ,然后执行以下操作

 $ git checkout-index --force --all 

这将在您的工作目录中重新创build文件,同时考虑到您的configuration更改和.gitattributes文件,并replace文本文件中所有可能被忽略的CRLF。

一旦完成,工作目录中的每个文本文件都将包含LF行结束符,并且git status仍然应该将workdir视为干净的。

要强制所有文本文件的LF行结束符,可以使用以下行(根据需要更改)在存储库的顶层创build.gitattributes文件:

 # Ensure all C and PHP files use LF. *.c eol=lf *.php eol=lf 

它确保Git认为是文本文件的所有文件都在存储库中具有规范化( LF )行结尾(通常是core.eolconfiguration控制默认情况下是哪一个)。

根据新的属性设置,任何包含CRLF的文本文件都应该由Git规范化。 如果这不会自动发生,您可以在更改行结束后手动刷新存储库,以便通过以下步骤(给定干净的工作目录)重新扫描并提交工作目录:

 $ echo "* text=auto" >> .gitattributes $ rm .git/index # Remove the index to force Git to $ git reset # re-scan the working directory $ git status # Show files that will be normalized $ git add -u $ git add .gitattributes $ git commit -m "Introduce end-of-line normalization" 

或按照GitHub文档 :

 git add . -u git commit -m "Saving files before refreshing line endings" git rm --cached -r . # Remove every file from Git's index. git reset --hard # Rewrite the Git index to pick up all the new line endings. git add . # Add all your changed files back, and prepare them for a commit. git commit -m "Normalize all the line endings" # Commit the changes to your repository. 

另见: @Charles Bailey的post 。

另外,如果你想排除任何文件不被视为文本,请取消它们的文本属性,例如

 manual.pdf -text 

或者将其明确标记为二进制:

 # Denote all files that are truly binary and should not be modified. *.png binary *.jpg binary 

要查看一些更高级的git规范化文件,请在Drupal核心中检查.gitattributes

 # Drupal git normalization # @see https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html # @see https://www.drupal.org/node/1542048 # Normally these settings would be done with macro attributes for improved # readability and easier maintenance. However macros can only be defined at the # repository root directory. Drupal avoids making any assumptions about where it # is installed. # Define text file attributes. # - Treat them as text. # - Ensure no CRLF line-endings, neither on checkout nor on checkin. # - Detect whitespace errors. # - Exposed by default in `git diff --color` on the CLI. # - Validate with `git diff --check`. # - Deny applying with `git apply --whitespace=error-all`. # - Fix automatically with `git apply --whitespace=fix`. *.config text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.css text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.dist text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.engine text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php *.html text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=html *.inc text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php *.install text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php *.js text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.json text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.lock text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.map text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.md text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.module text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php *.php text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php *.po text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.profile text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php *.script text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.sh text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php *.sql text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.theme text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 diff=php *.twig text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.txt text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.xml text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 *.yml text eol=lf whitespace=blank-at-eol,-blank-at-eof,-space-before-tab,tab-in-indent,tabwidth=2 # Define binary file attributes. # - Do not treat them as text. # - Include binary diff in patches instead of "binary files differ." *.gif -text diff *.gz -text diff *.ico -text diff *.jpeg -text diff *.jpg -text diff *.png -text diff *.phar -text diff *.exe -text diff *.svgz -text diff *.ttf -text diff 

也可以看看:

  • 在GitHub 处理行结尾
  • 使用vagrant时: Windows CRLF到Unix LF问题

从git 2.10开始,没有必要单独枚举每个文本文件。 Git 2.10将text = auto的行为与eol = lf一起修复 。 来源 。

.gitattributes文件在你的git仓库的根目录下:

 * text=auto eol=lf 

添加并提交它。

之后,您可以按照以下步骤进行操作,现在所有文件都已标准化:

 git rm --cached -r . # Remove every file from git's index. git reset --hard # Rewrite git's index to pick up all the new line endings. 

来源: 通过回答kenorb 。