我在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设置为native并core.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视为干净.
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的回答.
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)
此外,如果您要排除任何文件不被视为文本,请取消设置其文本属性,例如
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的规范化文件,检查.gitattributes在Drupal核心:
# 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)
也可以看看:
小智 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 次 |
| 最近记录: |