无法修复 git 上的行尾问题

本文关键字:问题 git | 更新日期: 2023-09-27 18:36:15

我和我的同事一直在为这个问题而苦苦挣扎。它有很好的文档记录(页面末尾的一些链接),但到目前为止我还没有能够解决这个问题。我们使用Visual Studio 2013在C#中编码。

每当我们合并两个分支时,我们都会有大量的"更改",其中文件被相同的文件完全替换。从我在网上读到的内容来看,我几乎可以肯定这是由于行尾的问题。

以下答案是帮助我最大的答案。我第一次按照这些步骤操作时,它只能找到一个要规范化的文件,即 .gitattributes 文件。但作为第一步,我用下面的文件替换了该文件,并找到了预期规范化的文件。这一切都是在我当地的分支机构完成的。

# Set the default behaviour, in case people don't have core.autocrlf set.
* text=auto
# Explicitly declare the text files you want to always be normalised and converted
# to native line endings on checkout.
*.cs text
*.json text
*.html text
*.csproj 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
*.gif binary

我完成了后续步骤,键入命令后收到了预期的消息(如下):"git add -u"

消息:

警告:CRLF 将在 (...) 中被 LF 取代

但是,当我切换到主分支并从本地分支更新时,再次替换了几个文件。我尝试在 master 分支中创建相同的 .gitattributes 文件并再次按照步骤操作,但在"git status"命令之后找不到应该规范化的文件,并且合并始终像以前一样执行,用相同的文件替换多个文件。

我做错了什么?

堆栈溢出线程

官方 github 解决方案

无法修复 git 上的行尾问题

问题是我没有将带有 gitattributes 文件的代码分支同步(推送)到存储库,我只是提交了它。因为我在当地工作,所以我认为这就足够了。但事实并非如此,合并正在获取以前版本的代码,而没有 gitattributes 文件。这个问题非常幼稚,但由于我上面引用的可用文档没有帮助,我将在下面发布我自己的教程,这可能会避免未来的 github 菜鸟犯同样的错误。我的教程主要基于此线程。

在本教程中,让我们假设存在一个工作分支和一个主分支。这个想法是将 gitattributes 文件推送到工作分支,下载主分支的代码并使用工作分支更新该代码。

# Add the following content to a file on the root of the repository in the 
# working branch, and name it .gitattributes
----------------------------------------------------------------------------
# Set the default behaviour, in case people don't have core.autocrlf set.
* text=auto
# Explicitly declare the text files you want to always be normalised and converted
# to native line endings on checkout.
*.cs text
*.json text
*.html text
*.csproj 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
*.gif binary
----------------------------------------------------------------------------
# From the root of the repository in the working branch remove everything from the index 
# (don't forget the '.')
git rm --cached -r .
# Re-add all the deleted files to the index
# (You should get lots of messages like:
#   warning: CRLF will be replaced by LF in <file>.)
git diff --cached --name-only -z | xargs -0 git add
# Commit
git commit -m "Fixed the line ending issue"
# Sync the code
# Switch to the master branch 
# Update (merge) from the working branch