Git切换分支时检测到无可见变更的修改,变更数量不稳定
Git分支切换时的异常变更检测问题
切换分支时Git会检测到文件变更,但执行git diff看不到可见内容,怀疑是不可见字符(如行尾换行符)导致的问题。强制切换到main分支再切回Prod分支后,变更仍然存在,但每次检测到的修改文件数量差异极大:
[Prod ≡ +0 ~974 -0 !][Prod ≡ +0 ~336 -0 !][Prod ≡ +0 ~223 -0 !][Prod ≡ +0 ~90 -0 !][Prod ≡ +0 ~959 -0 !]
脱敏后的控制台日志
> git clone https://redacted Project Cloning into 'Project'... remote: Azure Repos remote: Found 57799 objects to send. (385 ms) Receiving objects: 100% (57799/57799), 779.81 MiB | 54.44 MiB/s, done. Resolving deltas: 100% (47237/47237), done. Updating files: 100% (2975/2975), done. > cd .\\Project\\ [main ≡] > git checkout Prod Updating files: 100% (1597/1597), done. Switched to a new branch 'Prod' branch 'Prod' set up to track 'origin/Prod'. [Prod ≡ +0 ~974 -0 !] > git checkout main -f Updating files: 100% (1597/1597), done. Switched to branch 'main' Your branch is up to date with 'origin/main'. [main ≡] > git checkout Prod Updating files: 100% (1597/1597), done. Switched to branch 'Prod' Your branch is up to date with 'origin/Prod'. [Prod ≡ +0 ~336 -0 !] > git checkout main -f Updating files: 100% (1597/1597), done. Switched to branch 'main' Your branch is up to date with 'origin/main'. [main ≡] > git checkout Prod Updating files: 100% (1597/1597), done. Switched to branch 'Prod' Your branch is up to date with 'origin/Prod'. [Prod ≡ +0 ~223 -0 !] > git checkout main -f Updating files: 100% (1597/1597), done. Switched to branch 'main' Your branch is up to date with 'origin/main'. [main ≡] > git checkout Prod Updating files: 100% (1597/1597), done. Switched to branch 'Prod' Your branch is up to date with 'origin/Prod'. [Prod ≡ +0 ~90 -0 !] > git checkout main -f Updating files: 100% (1597/1597), done. Switched to branch 'main' Your branch is up to date with 'origin/main'. [main ≡] > git checkout Prod Updating files: 100% (1597/1597), done. Switched to branch 'Prod' Your branch is up to date with 'origin/Prod'. [Prod ≡ +0 ~959 -0 !] > git version git version 2.42.0.windows.1 [Prod ≡ +0 ~959 -0 !] > git diff <redacted>\\ILandDao.cs diff --git a/<redacted>/ILandDao.cs b/<redacted>/ILandDao.cs index e9e70eb3..3e403dd3 100644 --- a/<redacted>/ILandDao.cs +++ b/<redacted>/ILandDao.cs @@ -1,23 +1,23 @@ -using System.Collections.Generic; -using Project.Entity.Entities; - -namespace Project.Entity.Daos.Interfaces -{ - public interface ILandDao - { - #region methods - - #region public methods - - void Delete(LandEntity entity); - LandEntity GetById(int id); - - IList<LandEntity> GetList(); - - int Save(LandEntity entity); - - #endregion - - #endregion - } +using System.Collections.Generic; +using Project.Entity.Entities; + +namespace Project.Entity.Daos.Interfaces +{ + public interface ILandDao + { + #region methods + + #region public methods + + void Delete(LandEntity entity); + LandEntity GetById(int id); + + IList<LandEntity> GetList(); + + int Save(LandEntity entity); + + #endregion + + #endregion + } } \\ No newline at end of file > git config -l http.sslcainfo=C:/Program Files/Git/mingw64/etc/ssl/certs/ca-bundle.crt http.sslbackend=openssl diff.astextplain.textconv=astextplain filter.lfs.clean=git-lfs clean -- %f filter.lfs.smudge=git-lfs smudge -- %f filter.lfs.process=git-lfs filter-process filter.lfs.required=true credential.helper=manager core.editor="C:\\Program Files\\Notepad++\\notepad++.exe" -multiInst -notabbar -nosession -noPlugin core.autocrlf=true core.fscache=true core.symlinks=false pull.rebase=false credential.https://dev.azure.com.usehttppath=true init.defaultbranch=master http.proxy=http://<redacted> http.sslbackend=schannel https.proxy=http://<redacted> user.name=<redacted> user.email=<redacted> winupdater.recentlyseenversion=2.24.1.windows.2 filter.lfs.smudge=git-lfs smudge -- %f filter.lfs.process=git-lfs filter-process filter.lfs.required=true filter.lfs.clean=git-lfs clean -- %f credential.http://<redacted>.provider=generic diff.tool=vsdiffmerge difftool.prompt=true difftool.vsdiffmerge.cmd="C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsdiffmerge.exe" "$LOCAL" "$REMOTE" //t difftool.vsdiffmerge.keepbackup=false merge.tool=vsdiffmerge mergetool.prompt=true mergetool.vsdiffmerge.cmd="C:\\Program Files\\Microsoft Visual Studio\\2022\\Professional\\Common7\\IDE\\CommonExtensions\\Microsoft\\TeamFoundation\\Team Explorer\\vsdiffmerge.exe" "$REMOTE" "$LOCAL" "$BASE" "$MERGED" //m mergetool.vsdiffmerge.keepbackup=false mergetool.vsdiffmerge.trustexitcode=true credential.https://<redacted> gui.recentrepo=<redacted> gui.recentrepo=<redacted> gui.recentrepo=<redacted> core.repositoryformatversion=0 core.filemode=false core.bare=false core.logallrefupdates=true core.symlinks=false core.ignorecase=true remote.origin.url=https://<redacted> remote.origin.fetch=+refs/heads/*:refs/remotes/origin/* branch.main.remote=origin branch.main.merge=refs/heads/main branch.Prod.remote=origin branch.Prod.merge=refs/heads/Prod
问题推测与解决方案
该问题大概率是行尾换行符(EOL)不一致导致的。由于仓库未配置统一的换行符处理规则,不同分支或不同提交中的文件可能混用了LF和CRLF,导致Git在切换分支时误判文件变更。
建议在仓库根目录添加.gitattributes文件,统一配置文本文件的换行符处理规则,实现仓库级的标准化配置:
# Auto detect text files and perform LF normalization * text=auto *.cs text diff=csharp *.cshtml text diff=html *.csx text diff=csharp *.sln text eol=crlf *.csproj text eol=crlf
补充说明
- 该问题在办公室多台机器上均出现,因此需要仓库级配置而非单台机器的全局Git配置。
- 已提供完整的
git config -l输出、.gitattributes配置内容及Git状态摘要说明。
内容的提问来源于stack exchange,提问作者Chake
相关产品推荐
相关产品推荐

