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

如何筛选branch-A与branch-AA中代码变更相同的git commit,不依赖hash值

操作方法

核心判断逻辑是不使用包含提交人、提交时间、父提交信息的commit hash,而是使用仅和代码变更/最终代码内容相关的特征值做匹配,以下是具体操作步骤:

步骤1:提取第一个仓库branch-A的commit特征

进入tree-1仓库目录,执行以下操作:

  • 切换到目标分支:git checkout branch-A
  • 导出该分支所有commit的变更特征值(patch-id)、提交信息、原始commit hash到文件:
git log --reverse --pretty="%H %s" | while read commit_hash commit_msg; do
    git show $commit_hash | git patch-id | awk '{print $1 "\t" "'"$commit_msg"'" "\t" "'"$commit_hash"'"}'
done > ../branch-A-commit-info.txt

patch-id是Git基于commit的diff内容生成的哈希值,只要代码变更内容完全相同,无论commit hash、提交人、提交时间是否一致,patch-id都会完全相同。

步骤2:提取第二个仓库branch-AA的commit特征

进入tree-2仓库目录,执行以下操作:

  • 切换到目标分支:git checkout branch-AA
  • 同样导出分支commit特征到文件:
git log --reverse --pretty="%H %s" | while read commit_hash commit_msg; do
    git show $commit_hash | git patch-id | awk '{print $1 "\t" "'"$commit_msg"'" "\t" "'"$commit_hash"'"}'
done > ../branch-AA-commit-info.txt

步骤3:匹配两个分支中变更完全相同的commit

在两个导出文件的父目录执行以下命令,即可输出匹配结果:

awk 'NR==FNR {
    match_map[$1] = $0
    next
}
$1 in match_map {
    print "匹配成功:\nbranch-A侧:" match_map[$1] "\nbranch-AA侧:" $0 "\n---"
}' branch-A-commit-info.txt branch-AA-commit-info.txt

可选:更严格的内容一致校验

如果需要校验两个commit的最终目录和所有文件内容100%完全一致,可以对比commit的tree hash,该值仅和当前commit对应的整个仓库的文件内容、目录结构相关,获取命令为:
git show -s --pretty=%T <你的commit hash>
只要两个commit的tree hash相同,对应代码内容就完全一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.25 16:45:02