如何在git回购和工作副本中强制LF EOL?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (62)

我在GitHub上有一个Git存储库。许多文件最初是在Windows上开发的,我对行尾并不太小心。当我执行初始提交时,我也没有任何git配置来强制执行正确的行尾。结果是,我在我的GitHub存储库中有一些带有CRLF行结尾的文件。

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

我已经设置了一个.gitattributes文件包含text eol=LF这是对的吗?有了那份承诺和压力,我能不能rm我的本地回购和重新克隆从GitHub获得预期的效果?

提问于
用户回答回答于

如果没有关于存储库中的文件(纯源代码、图像、可执行文件,...)的一些信息,就很难回答以下问题:)

此外,我将考虑您愿意在你的工作目录中默认为LF作为行结束,因为愿意确保文本文件中的.git存储库中有LF行结束,无论是在Windows还是Linux上工作。确实安全总比抱歉好..。

但是,还有一个更好的选择:Linux Workdir中的LF行结束、Windows workdir中的CRLF行结束和存储库中的LF行结束。

当在Linux和Windows上部分工作时,请确保core.eol设置为nativecore.autocrlf设置为true.

然后,替换.gitattributes使用以下文件存档

* text=auto

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

但是,正如你知道存储库的内容一样,可以帮助Git并帮助他从二进制文件中检测文本文件。

如果从事基于C的图像处理项目,请替换.gitattributes使用以下文件存档

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

这将确保扩展名为c、h或txt的文件将与你的repo中的LF行结尾一起存储,并在工作目录中有本机行尾。JPEG文件不会被碰。所有其他的将受益于相同的自动过滤,如上文所示。

为了更深入地了解这一切的内在细节,我建议你深入到这个非常好的帖子中去。

用户回答回答于

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

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

它确保Git认为是文本文件的所有文件都已标准化(LF)存储库中的行尾(通常为core.eol默认情况下,配置控制有哪些)。

根据新的属性设置,任何包含CRLFs的文本文件都应该由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"

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.

此外,如果希望排除任何不被视为文本的文件,请取消设置它们的text属性。

manual.pdf      -text

或者将其显式标记为二进制:

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

要查看更高级的git规范化文件,请检查.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
*.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

扫码关注云+社区