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

如何在不修改Git仓库的前提下获取多个指定提交的压缩diff?

符合要求的Git别名实现方案

最终可直接使用的别名配置

你可以执行以下命令直接配置全局别名:

git config --global alias.squash-diff '!f() { if [ $# -eq 0 ]; then echo "请传入需要查看的提交哈希列表" >&2; exit 1; fi; sorted_commits=$(git rev-list --reverse --topo-order "$@"); if [ -z "$sorted_commits" ]; then echo "未找到有效提交" >&2; exit 1; fi; first_commit=$(echo "$sorted_commits" | head -n1); if git rev-parse --verify "${first_commit}^" >/dev/null 2>&1; then parent_commit="${first_commit}^"; else parent_commit=$(git hash-object -t tree /dev/null); fi; temp_index=$(mktemp); rm -f "$temp_index"; export GIT_INDEX_FILE="$temp_index"; trap "rm -f \"$temp_index\"; unset GIT_INDEX_FILE" EXIT; git read-tree "$parent_commit"; for commit in $sorted_commits; do if ! git cherry-pick --no-commit --strategy=recursive --strategy-option=fail "$commit" >/dev/null 2>&1; then echo "错误:应用提交 $commit 时出现冲突,选中提交之间存在无关提交修改了相同文件" >&2; exit 1; fi; done; git diff --cached "$parent_commit"; }; f'

使用方法

直接指定提交哈希使用

git squash-diff <提交1> <提交2> <提交3> ...

配合工单前缀批量使用

如果你需要查看前缀为PROJECT-123的所有工单提交的总diff,可以直接执行:

git log --grep="^PROJECT-123" --format=%H | xargs git squash-diff

实现说明

  • 全程使用独立临时索引文件操作,不会修改当前仓库的工作树、原有索引、分支引用等任何内容,完全符合只读要求
  • 提交会自动按拓扑顺序从早到晚应用,避免应用顺序错误导致的异常
  • 开启冲突立即失败策略,只要选中的提交应用过程中出现冲突(即存在无关提交修改了相同文件),就会立即输出错误信息并以状态码1退出
  • 执行完成后自动清理临时文件,不会留下冗余文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 05:39:04