Git 在行标准化后插入 unicode 字符

标签 git unicode tfs mingw

这是一个非常奇怪的情况,我希望有人能提供帮助。我在 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++ 中观察到:

Unicode madness

请注意,CR 和 LF 似乎也分为多行 - “CR”行正在覆盖文件中的现有行。

当我关闭“显示所有字符”时,只有 CR 和 LF 消失 - 文件中的所有其他内容都是物理字符。这些文件还显示为“在 UCS-2 LE BOM 中编码”,而不是我所期望的“UTF-8 BOM”:

File Encoding

我尝试过的

我查找了发生这种情况的提交,但它不存在。如果我重置回我知道其中包含原始文件内容的提交,则没有任何变化 - 无论我重置为什么提交,文件都保持原样。

还需要注意的是,当我通过 TFS UI 查看文件时,它看起来不错:

TFS

而且提交历史如我所料 - 最后一次提交是在几个小时前(这是我一直试图重置的那个)。

我可以通过 TFS UI 上的下载按钮下载受影响的文件,它们看起来很好。

我试图覆盖我本地存储库中的一些下载文件 - 希望我可以通过另一个提交来解决它,但是 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")

然后,当我尝试添加“已修改”文件时,它只报告以下内容:

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

虽然这可能有效,但问题是有很多文件,如果可以的话,我真的不想手动重建存储库!

我也尝试过完全删除我的存储库并重新克隆,但文件在重新克隆后保持不变。我也尝试过克隆到磁盘上的其他位置。

我刚刚也在另一台机器上测试了克隆存储库 - 那里也发生了同样的行为 - 所以这肯定与 Remote 有关。

最后,值得注意的是这些文件存在于名为 feature/code-policies 的分支中。我试过从那个分支分支出来,看看是不是分支本身有问题,但是没有什么区别。我假设分支本身有问题?

有没有人见过这样的事情?有谁知道可能会发生什么,以及我如何能够解决问题?

最佳答案

假设您在 Windows 计算机上工作。 Dealing with line endings ,您可以配置全局设置或每个存储库设置。

  • 行尾的全局设置

在 Windows 上,您只需将 true 传递给配置即可。例如:

git config --global core.autocrlf true
# Configure Git on Windows to properly handle line endings 
  • 每个存储库设置

可选地,您可以通过配置特殊的 .gitattributes 文件来配置 Git 在每个存储库的基础上管理行尾的方式。 .gitattributes 文件必须在存储库的根目录中创建并像任何其他文件一样提交。

这是一个示例 .gitattributes 文件。您可以将其用作存储库的模板:

# 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"

检查您的步骤是否正确。

关于Git 在行标准化后插入 unicode 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38880691/

相关文章:

git - 如何在 visualstudio.com 的命令行上输入 git 凭据?

git - 如何检查远程 Git 存储库的状态?

python - "ASCII-ish"Python 中一些 Unicode 字符的近似值

svg - 在什么情况下,一种字体的每个 unicode 字符可以有多个字形,这是如何处理的?

c# - TFS REST API .NET 客户端,TFS2017,个人访问 token : "You must pass a valid patch document in the body of the request."

git - 无法在 jenkins 中过滤 github webhook 触发器

python - UnicodeEncodeError : 'ascii' codec can't encode character u'\xe9' in position 7: ordinal not in range(128)

tfs - 如何以类似 Visual Studio 的形式查看 MS Project 中的 TFS 工作项

visual-studio - 在 Visual Studio 中使用 selenium 引用编写的测试构建创建失败

git - 您可以手动设置 Jenkins currentBuild.changeSets 吗?