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

Cho*_*ett 153 git github eol

我在github上托管了一个git存储库.许多文件最初是在Windows上开发的,我对行结尾并不太谨慎.当我执行初始提交时,我也没有任何git配置来强制执行正确的行结束.结果是我的github存储库中有许多带有CRLF行结尾的文件.

我现在部分在Linux上开发,我想清理行结尾.如何确保文件在github上使用LF正确存储,并在我的工作副本中使用LF?

我已经设置了一个.gitattributes包含text eol=LF; 那是对的吗?有了这个承诺和推动,我可以只是rm我的本地仓库并从github重新克隆以获得所需的效果吗?

nul*_*ken 213

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

除此之外,我会认为您愿意默认LF作为工作目录中的行结尾,因为您愿意确保文本文件在您的.git存储库中具有LF行结尾,而您在Windows或Linux上工作.确实比抱歉更安全....

然而,有一个更好的选择:从你的Linux WORKDIR LF行结束,CRLF行结束您的Windows WORKDIR并在你的仓库LF行结束效益.

由于您正在部分使用Linux和Windows,请确保core.eol设置为nativecore.autocrlf设置为true.

然后,使用以下内容替换.gitattributes文件的内容

* text=auto
Run Code Online (Sandbox Code Playgroud)

这将让Git在提交和签出时为您处理自动化行结尾转换.二进制文件不会被更改,检测为文本文件的文件将看到即时转换的行结尾.

但是,正如您所知道的存储库内容,您可以帮助Git帮助他检测二进制文件中的文本文件.

如果您使用基于C的图像处理项目,请.gitattributes使用以下内容替换文件的内容

* text=auto
*.txt text
*.c text
*.h text
*.jpg binary
Run Code Online (Sandbox Code Playgroud)

这将确保扩展名为c,h或txt的文件将与您的仓库中的LF行结尾一起存储,并在工作目录中具有本机行结尾.不会触及Jpeg文件.所有其他人将受益于如上所述的相同的自动过滤.

为了得到一个得到这一切的内部细节更深入的了解,我建议你潜入这个非常好岗位"当心你的行末"蒂姆克莱姆,一个Githubber.

作为一个真实的例子,您还可以查看此提交,其中.gitattributes演示了对文件的更改.

考虑以下评论,更新答案

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

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

对存储库运行以下命令

$ git config core.eol lf
$ git config core.autocrlf input
Run Code Online (Sandbox Code Playgroud)

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

或者,你可以帮助的Git分清哪些通过创建一个文本文件.gitattributes包含类似于下面的内容的文件:

# 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
Run Code Online (Sandbox Code Playgroud)

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

最后,确保git status提到"无需提交(工作目录清理)",然后执行以下操作

$ git checkout-index --force --all
Run Code Online (Sandbox Code Playgroud)

这将在您的工作目录中重新创建您的文件,考虑您的配置更改和.gitattributes文件,并替换文本文件中任何可能被忽略的CRLF.

完成此操作后,工作目录中的每个文本文件都将承载LF行结尾,并且git status仍应将workdir视为干净.

  • 我实际上不希望在我的Windows目录中使用CRLF,因为我的Linux环境实际上是一个共享Windows目录的VirtualBox; 虽然Notepad ++等只能在Windows上处理LF,但是`vi`对CRLF不太满意.我只是想改变它,以便`core.autocrlf`是'false`(或`input`)? (26认同)
  • @CMCDragonkai根据你的shell,`git checkout-index --force --all`可能会更好.关于原始问题,第二点看起来有点偏离主题.问一个专门的问题怎么样? (8认同)
  • 我不明白为什么.gitattributes无法处理在Linux和Windows之间共享工作副本的情况.我们不能设置`text`和`eol = lf`来通过`core.eol`和`core.autocrlf`获得与你的答案中描述的相同的结果吗? (8认同)
  • `git checkout-index --force --all`对我没有任何帮助.有效的是[GitHub说明中的命令列表](https://help.github.com/articles/dealing-with-line-endings/#refreshing-a-repository-after-changing-line-endings)处理这个问题. (7认同)
  • 很好的答案.使用此设置的其他任何人的快速注释:"*text = auto"行应该是.gitattributes文件中的第一行,以便后续行可以覆盖该设置. (5认同)
  • 这个答案是旧的。你想要@koppor 的答案。`.gitattributes` 中的单行:`* text=auto eol=lf` (2认同)

kop*_*por 96

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

.gitattributes 在git存储库的根目录中的文件:

* text=auto eol=lf
Run Code Online (Sandbox Code Playgroud)

添加并提交它.

之后,您可以执行以下步骤,现在所有文件都已规范化:

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.
Run Code Online (Sandbox Code Playgroud)

资料来源:kenorb的回答.

  • Git 2.10已于2016年9月3日发布. (5认同)

ken*_*orb 23

要强制所有文本文件的LF行结尾,您可以.gitattributes使用以下行在存储库的顶级创建文件(根据需要进行更改):

# Ensure all C and PHP files use LF.
*.c         eol=lf
*.php       eol=lf
Run Code Online (Sandbox Code Playgroud)

这确保了Git认为是文本文件的所有文件LF在存储库中都有normalized()行结尾(通常是core.eol配置控件,默认情况下你有哪一个).

根据新的属性设置,任何包含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"
Run Code Online (Sandbox Code Playgroud)

或者根据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.
Run Code Online (Sandbox Code Playgroud)

另见:@Charles Bailey帖子.

此外,如果您要排除任何文件不被视为文本,请取消设置其文本属性,例如

manual.pdf      -text
Run Code Online (Sandbox Code Playgroud)

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

# Denote all files that are truly binary and should not be modified.
*.png binary
*.jpg binary
Run Code Online (Sandbox Code Playgroud)

看到一些更高级的git的规范化文件,检查.gitattributesDrupal核心:

# 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
*.svg     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."
*.eot     -text diff
*.exe     -text diff
*.gif     -text diff
*.gz      -text diff
*.ico     -text diff
*.jpeg    -text diff
*.jpg     -text diff
*.otf     -text diff
*.phar    -text diff
*.png     -text diff
*.svgz    -text diff
*.ttf     -text diff
*.woff    -text diff
*.woff2   -text diff
Run Code Online (Sandbox Code Playgroud)

也可以看看:

  • 1.`text = auto`有误导性.你不能一起使用`text = auto`和`eol`.设置`eol`禁用自动检测文本文件.这就是您必须指定所有这些文件类型的原因.如果启用了"auto",则不需要全部.2.你不需要`text`和`eol = lf`.`eol = lf`有效地设置`text`. (2认同)
  • @Ben说,如果你没有明确标记所有二进制文件,这个配置目前是错误和危险的. (2认同)
  • 重要的是要注意,@ Ben所说的不再正确,它始终是一个错误-并非预期的行为。 (2认同)

小智 8

我将 Chromium 克隆depot_tools到我的 mac 上,工作副本的所有文件都以 CRLF 结尾。我发现这个脚本解决了我的问题。

cd <your repo>
# config the local repo to use LF
git config core.eol lf
git config core.autocrlf input

# Copy files from the index to the working tree
git checkout-index --force --all

# If above line doesn't work, delete all cached files and reset.
git rm --cached -r .
git reset --hard
Run Code Online (Sandbox Code Playgroud)


归档时间:

查看次数:

80442 次

最近记录:

7 年,10 月 前