这是一个非常奇怪的情况,我希望有人能帮忙。我在tfs 2015中使用git,我一直在尝试规范化存储库中的行尾。
背景
我一直在尝试以下各种组合:

git rm --cached -r .
git reset --hard


git rm --cached -r .
git add --all .
git commit -am "commit msg"

我已经运行了很多次这些命令,同时还使用了git config --global core.autocrlf设置。我只试过truefalse-我没有用过input
我也一直在添加/删除和修改我的.gitattributes文件,同时尝试所有这些。
问题
我注意到,在上次提交之后,repo中的一些文件现在每隔一行就被大量unicode字符替换。在打开“显示所有字符”的Notepad++中观察到:
git - Git行规范化后插入unicode字符-LMLPHP
注意,CRS和LFS似乎也在多条线路上分开,“CR”行覆盖文件中的现有行。
当我关闭“显示所有字符”时,只有crs和lfs消失-文件中的其他所有字符都是物理字符。文件也显示为“在ucs-2le bom中编码”,而不是“utf-8bom”,如我所料:
git - Git行规范化后插入unicode字符-LMLPHP
我试过的
我寻找这件事发生的承诺,但它不存在。如果我重置回一个我知道包含原始文件内容的提交,则没有任何更改-无论我重置到什么提交,文件都保持原样。
还要注意的是,当我通过tfs ui查看文件时,它看起来很好:
git - Git行规范化后插入unicode字符-LMLPHP
提交历史记录和我预期的一样——上一次提交是在几个小时前(这是我一直试图重置的一次)。
我可以通过tfs用户界面上的下载按钮下载受影响的文件,它们看起来很不错。
我试图覆盖本地repo中的一些下载文件-希望我可以用另一个提交来解决,但是git抱怨行结尾,并且文件没有暂存。
ME@MYMACHINE MINGW64 /d/git/CLIENT.Core.ProjectTemplates (feature/code-policies)
$ git st
On branch feature/code-policies
Your branch is up-to-date with 'origin/feature/code-policies'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Powershell/TFS/templates/buildDefinitions/CLIENT/CLIENT.WCFService-CI.json

no changes added to commit (use "git add" and/or "git commit -a")

ME@MYMACHINE MINGW64 /d/git/CLIENT.Core.ProjectTemplates (feature/code-policies)
$ git commit -am "testing overwrite"
warning: LF will be replaced by CRLF in Powershell/TFS/templates/buildDefinitions/CLIENT/CLIENT.WCFService-CI.json.
The file will have its original line endings in your working directory.
On branch feature/code-policies
Your branch is up-to-date with 'origin/feature/code-policies'.
nothing to commit, working directory clean

ME@MYMACHINE MINGW64 /d/git/CLIENT.Core.ProjectTemplates (feature/code-policies)
$ git st
On branch feature/code-policies
Your branch is up-to-date with 'origin/feature/code-policies'.
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   Powershell/TFS/templates/buildDefinitions/CLIENT/CLIENT.WCFService-CI.json

no changes added to commit (use "git add" and/or "git commit -a")

然后,当我尝试添加“modified”文件时,它只报告以下内容:
ME@MYMACHINE MINGW64 /d/git/CLIENT.Core.ProjectTemplates (feature/code-policies)
$ git add --all .
warning: LF will be replaced by CRLF in Powershell/TFS/templates/buildDefinitions/CLIENT/CLIENT.WCFService-CI.json.
The file will have its original line endings in your working directory.

ME@MYMACHINE MINGW64 /d/git/CLIENT.Core.ProjectTemplates (feature/code-policies)
$ git st
On branch feature/code-policies
Your branch is up-to-date with 'origin/feature/code-policies'.
nothing to commit, working directory clean

虽然这可能有效,但问题是有很多文件,如果我能帮忙的话,我真的不想手动重建回购协议!
我也尝试过完全删除我的repo并重新克隆,但重新克隆后文件保持不变。我也试着克隆到磁盘上的另一个位置。
我刚刚在另一台机器上测试了复制回购协议——同样的行为也会发生——所以这肯定与遥控器有关。
最后,值得注意的是这些文件存在于一个称为特性/代码策略的分支中。我试着把那根树枝剪掉,看看树枝本身有没有问题,但没什么区别。我想树枝本身也有腐败的地方吧?
以前有人见过这样的东西吗?有人知道会发生什么事吗?我怎样才能解决这个问题?

最佳答案

假设你在windows机器上工作。Dealing with line endings,您可以配置全局设置或每个存储库设置。
行结束的全局设置
在windows上,只需将true传递给配置。例如:

git config --global core.autocrlf true
# Configure Git on Windows to properly handle line endings

每个存储库设置
或者,您可以通过配置一个特殊的.gittattributes文件来配置git在每个存储库基础上管理行尾的方式。.gittattributes文件必须在存储库的根目录中创建,并像任何其他文件一样提交。
下面是一个示例.gittattributes文件。您可以将其用作存储库的模板:
# Set the default behavior, in case people don't have core.autocrlf set.
* text=auto

# Explicitly declare text files you want to always be normalized and converted
# to native line endings on checkout.
*.c text
*.h text

# Declare files that will always have CRLF line endings on checkout.
*.sln text eol=crlf

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

更改行尾后刷新存储库
将当前文件保存在git中,这样就不会丢失任何工作。
git add . -u
git commit -m "Saving files before refreshing line endings"

从git的索引中删除每个文件。
git rm --cached -r .

重写git索引以获取所有新行结尾。
git reset --hard

将所有更改的文件添加回,并为提交做好准备。这是您检查哪些文件(如果有的话)没有更改的机会。
git add .
# It is perfectly safe to see a lot of messages here that read
# "warning: CRLF will be replaced by LF in file."

将更改提交到存储库。
git commit -m "Normalize all the line endings"

检查你的步骤是否正确。

09-04 14:00
查看更多