为什么源代码树不 Git 区分 Powershell 脚本 (ps1)

Why does Source Tree not Git Diff a Powershell Script (ps1)

我在 Windows (v1.8.3.0) 上使用 SourceTree 作为我的 git 客户端。出于某种原因,它没有显示我的 PowerShell 脚本 (.ps1) 的 diff,而是将其显示为二进制文件:

是否需要切换设置才能获得正确的行为?如果我将文件重命名为 .txt,那么 diff 会按预期工作。

我的存储库中有一个 .gitattributes 文件,我已经更新它以明确说明 .ps1 文件,但这似乎也不起作用:

*.ps1   diff=astextplain
*.PS1   diff=astextplain

或者,我可以在转到 External Diff 时看到 diff 很好(通过 BeyondCompare),但是,这并不能帮助我根据特定行号创建提交:

这似乎是 Atlassian 产品(BitBucket、SourceTree)的一个已知问题:https://bitbucket.org/site/master/issues/5453/powershell-module-manifest-file-psd1-are

问题是我的 .ps1 文件被保存为 UTF16 Unicode。将其更改为 UTF8ascii 修复此问题,我再次在 SourceTree 中获得 diffs。

你可以做到(仅限最近的 git!)

*.ps1 text working-tree-encoding=UTF-16LE eol=CRLF

(如果有 bom 等,请删除 LE)

这会进入您的 gitattributes 文件