如何规范化 Git 中的工作树线结尾?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15641259/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to normalize working tree line endings in Git?
提问by user11171
I have cloned a repository that had inconsistend line endings. I have added a .gitattributes
that sets the text attribute for the files I want to normalize. Now when I commit changes I get the message:
我克隆了一个行尾不一致的存储库。我添加了一个.gitattributes
为我想要规范化的文件设置文本属性。现在,当我提交更改时,我收到消息:
warning: CRLF will be replaced by LF in FILE.
The file will have its original line endings in your working directory.
How can I make git normalize my working copy of the file for me? Preferably I would like git to normalize the entire working tree.
如何让 git 为我规范化我的文件工作副本?最好我希望 git 规范化整个工作树。
回答by John Szakmeister
For those using v2.16 or better, you can simply use:
对于使用 v2.16 或更高版本的用户,您可以简单地使用:
git add --renormalize . # Update index with renormalized files
git status # Show the files that will be normalized
git commit -m "Introduce end-of-line normalization"
These directions are straight out of the gitattributes. For older versions, the docs(prior to v2.12) provide a different answer:
这些方向直接来自gitattributes。对于旧版本,文档(v2.12 之前)提供了不同的答案:
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"
Do this sequence after you have edited .gitattributes
.
编辑后执行此序列.gitattributes
。
Update
更新
It appears some users have had trouble with the above instructions. Updated docs for gitattributes(2.12 to 2.14) shows a new set of instructions (after editing the .gitattributes files):
似乎有些用户在上述说明中遇到了问题。gitattributes 的更新文档(2.12 到 2.14)显示了一组新的指令(在编辑 .gitattributes 文件之后):
git read-tree --empty # Clean index, force re-scan of working directory
git add .
git status # Show files that will be normalized
git commit -m "Introduce end-of-line normalization"
Thanks to @vossad01for pointing this out.
感谢@vossad01指出这一点。
Also, with either solution the files in your working copy still retain their old line endings. If you want to update them, make sure your working tree is cleanand use:
此外,无论使用哪种解决方案,您的工作副本中的文件仍保留其旧的行尾。如果要更新它们,请确保您的工作树是干净的并使用:
git rm --cached -r .
git reset --hard
Now the line endings will be correct in your working tree.
现在,您的工作树中的行尾将是正确的。
回答by philippn
With Git client 2.16 and higher there is now a much simpler way to do this. Just use:
使用 Git 客户端 2.16 及更高版本,现在有一种更简单的方法来做到这一点。只需使用:
git add --renormalize .
Note: it's better to do this with a clean workspace. For details, see:
注意:最好在干净的工作区中执行此操作。有关详细信息,请参阅:
回答by gavenkoa
Alternative approach (differs only in command used)
替代方法(仅在使用的命令上有所不同)
Make sure you have no any pending changes in repository:
确保存储库中没有任何待处理的更改:
$ git status
$ git stash
Modify .gitattributes
so CRLF interpretation will changed:
修改.gitattributes
以便 CRLF 解释将改变:
$ echo "*.txt text" >>.gitattributes
$ git commit -m "Made .txt files a subject to CRLF normalization." -- .gitattributes
Remove data from index and refresh working directory:
从索引中删除数据并刷新工作目录:
$ git rm --cached -r .
$ git reset --hard
Review CRLF fixes that Git proposes:
查看 Git 提出的 CRLF 修复:
$ git ls-files --eol
$ git status
$ git diff
Agree with Git decision:
同意 Git 的决定:
$ git add -u
$ git commit -m "Normalized CRLF for .txt files"
Reload changes as if clean clone was done:
像完成干净的克隆一样重新加载更改:
$ git rm --cached -r .
$ git reset --hard
回答by vonbrand
The .gitattributes
settings will only affect new commits. If this repository has nohistory published (no others depending on it), you might want to go through the whole history. In Unix/Linux, you can use dos2unix(1)
to fix all files in combination with find(1)
, and using the history rewriting of filter-branch
(see the discussionin the git book) you can even clean up the full history of the project.
这些.gitattributes
设置只会影响新的提交。如果这个存储库没有发布历史(没有其他人依赖它),你可能想要浏览整个历史。在 Unix/Linux 中,您可以使用dos2unix(1)
来修复所有文件find(1)
,并且使用 的历史重写filter-branch
(参见git 书中的讨论)您甚至可以清理项目的完整历史。
Use with utmost care, on a fresh clone. Get in contact with anybodywho might have a clone, and advise them what you want to do.
在新克隆上小心使用。与任何可能拥有克隆的人取得联系,并告知他们您想做什么。
回答by Walter Laan
The * text=auto option in .gitattributes leaves the Git repository in an 'illegal state' if it contains files with CRLF (Windows) line endings which are now marked as text (see https://marc.info/?l=git&m=154484903528621&w=2). The standard renormalize option does not work correctly with the LFS filters, so the instructions in the other answers or for example at https://help.github.com/en/articles/dealing-with-line-endings, do not work correctly. Instead these steps worked for us:
如果 .gitattributes 中的 * text=auto 选项包含带有 CRLF (Windows) 行结尾的文件,这些文件现在被标记为文本(请参阅https://marc.info/?l=git&m =154484903528621&w=2)。标准 renormalize 选项无法与 LFS 过滤器一起正常工作,因此其他答案或例如https://help.github.com/en/articles/dealing-with-line-endings 中的说明无法正常工作. 相反,这些步骤对我们有用:
Situation:
情况:
- On Windows
- Git repository contained files with both CR and CRLF line endings
- Added * text=auto to .gitattributes (so not depended on user having set core.crlf=auto on Windows)
Also changed the -crlf to -text for LFS tracked files, not sure that is needed.
- Create a new branch from the branch with the line ending problem (assuming no uncommitted changes there): git checkout -b feature/doing-stuff-fix-eol
- Remove the LFS filters from .gitattributes (replace all 'filter=lfs diff=lfs merge=lfs ' with nothing)
- Commit and push: git commit -a -m "Disable LFS filters for EOL fix"
- Move to non-git folder
- Uninstall LFS globally: git lfs uninstall
- Create a new repository clone: git clone -b feature/doing-stuff-fix-eol [remote repository url] fix-eol
- Normalize the line endings: git add --renormalize . (note the dot to renormalize all files)
- Check only the correct files normalized. It should not include files normally handled by LFS!
- Commit and push (save the hash): git commit -m "Fix line endings"
- Move to non-git folder
- Install LFS globally: git lfs install
- Go to original repository clone and pull
- Checkout your original branch: git checkout feature/doing-stuff
- Cherry pick the eol fix commit and push: git cherry-pick [hash]
- Delete the eol branch and push
- Delete the eol repository clone (or keep around if you need to fix more branches)
- 在 Windows 上
- Git 存储库包含具有 CR 和 CRLF 行结尾的文件
- 将 * text=auto 添加到 .gitattributes (因此不依赖于用户在 Windows 上设置了 core.crlf=auto)
还将 LFS 跟踪文件的 -crlf 更改为 -text,不确定是否需要。
- 从具有行尾问题的分支创建一个新分支(假设那里没有未提交的更改): git checkout -b feature/doing-stuff-fix-eol
- 从 .gitattributes 中删除 LFS 过滤器(将所有 'filter=lfs diff=lfs merge=lfs ' 替换为空)
- 提交并推送: git commit -a -m "禁用 LFS 过滤器以进行 EOL 修复"
- 移动到非 git 文件夹
- 全局卸载 LFS: git lfs uninstall
- 创建一个新的仓库克隆: git clone -b feature/doing-stuff-fix-eol [remote repository url] fix-eol
- 规范化行尾: git add --renormalize 。(注意重新规范化所有文件的点)
- 仅检查标准化的正确文件。它不应该包括通常由 LFS 处理的文件!
- 提交和推送(保存哈希): git commit -m "Fix lineendings"
- 移动到非 git 文件夹
- 全局安装 LFS: git lfs install
- 转到原始存储库克隆并拉取
- 结帐您的原始分支: git checkout 功能/正在做的事情
- Cherry pick the eol fix commit and push: git cherry-pick [hash]
- 删除 eol 分支并推送
- 删除 eol 存储库克隆(如果需要修复更多分支,请保留)