You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让git log --follow像git diff一样显示二进制文件的补丁

解决Git log --follow --patch不显示二进制文件自定义diff的问题

针对你的场景(.ods表格文件,已配置spreadsheet自定义diff驱动),以下是让git log --follow --patch和git diff使用相同diff机制的方法:

1. 确保自定义diff命令适配Git的参数格式

Git调用自定义diff命令时,会传递7个固定参数:

<command> <旧文件路径> <旧文件哈希> <旧文件权限> <新文件路径> <新文件哈希> <新文件权限>

你的excelcompare工具需要能处理这些参数,并且输出标准的unified diff格式(带@@ ... @@标记的文本差异)。如果工具默认输出不符合要求,你可以写一个shell脚本做中间转换,比如:

创建脚本git-spreadsheet-diff.sh(记得赋予执行权限chmod +x):

#!/bin/bash
# 提取实际需要的旧文件和新文件路径
OLD_FILE="$1"
NEW_FILE="$5"

# 调用excelcompare并转换输出为Git兼容的unified diff格式
excelcompare "$OLD_FILE" "$NEW_FILE" | awk '
    BEGIN { print "diff --git a/'$OLD_FILE' b/'$NEW_FILE'" }
    /^---/ { print }
    /^\+\+\+/ { print }
    /^-/ { print "-" $0 }
    /^\+/ { print "+" $0 }
    # 根据工具实际输出调整其他行的处理逻辑
'

然后修改.git/config中的命令路径:

[diff "spreadsheet"]
    binary = true
    command = /绝对路径/git-spreadsheet-diff.sh

2. 验证配置并测试

执行以下命令测试单条提交的diff显示:

git log -1 --patch your_file.ods

如果能显示文本化的补丁内容,说明配置生效。此时再执行git log --follow --patch your_file.ods,就能看到追踪文件重命名后的文本diff历史了。

3. 排查常见问题

如果仍显示二进制文件差异,检查以下几点:

  • .gitattributes文件在仓库根目录,且未被Git忽略(执行git check-attr diff your_file.ods可验证配置是否生效)
  • 自定义脚本/工具路径正确,且有执行权限
  • 工具输出确实是标准unified diff格式(可直接运行脚本传入两个测试文件,查看输出是否符合要求)

内容的提问来源于stack exchange,提问作者chichak

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 04:50:11