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

使用git filter-repo后同一提交哈希对应不同树哈希的异常

Git filter-repo --replace-text 引发的提交哈希与树内容不匹配异常

在使用git filter-repo工具的--replace-text选项改写仓库后,出现了违反Git基本公理的异常:两个仓库中存在相同的提交哈希,但对应的树哈希和树内容完全不同(排除极难复现的哈希碰撞情况),且该问题可通过简单步骤复现。

复现步骤

  1. 初始化名为test_repo的仓库,创建如下提交历史:
$ git log -p
commit 00fc46c3372592059ff63bc0dfb610d8e583fa3e (HEAD -> master)
Author: REDACTED
Date:   Thu Mar 9 10:04:38 2023 +0000

    second change

diff --git a/foo b/foo
index 8d5d568..243cf01 100644
--- a/foo
+++ b/foo
@@ -1,2 +1,4 @@
 initial
 
+zzzzz
+

commit 19f6a7da7e8d9ca30a644872167bd7f17c3b5f92
Author: REDACTED
Date:   Thu Mar 9 10:04:23 2023 +0000

    blah

diff --git a/foo b/foo
new file mode 100644
index 0000000..8d5d568
--- /dev/null
+++ b/foo
@@ -0,0 +1,2 @@
+initial
+
  1. 创建文本替换文件repl_file,内容为:
zzz==>yyyy
  1. 克隆仓库并执行改写操作:
git clone . ../test_repo_mod
cd ../test_repo_mod
git filter-repo --replace-text ../test_repo/repl_file

改写后初始提交哈希保持不变(符合预期,因为替换未影响它),第二个提交哈希变更,内容更新为:

$ git show
commit a2b5518f99f3a42d55931021e4a79c59f0971734 (HEAD -> master)
Author: REDACTED
Date:   Thu Mar 9 10:04:38 2023 +0000

    second change

diff --git a/foo b/foo
index 8d5d568..5d94489 100644
--- a/foo
+++ b/foo
@@ -1,2 +1,4 @@
 initial
 
+yyyyzz
+

异常现象

将原仓库历史拉取到改写后的仓库后,出现异常:

$ git remote add prefilter ../test_repo
$ git fetch prefilter
[output removed]

$ git log --graph --all  --pretty=format:'(%D) H:%H - T:%T' 
* (master) H:a2b5518f99f3a42d55931021e4a79c59f0971734 - T:0258b53a79efebb3a1a18fae2f5f2b26338bfaf8
| * (HEAD, replaced, prefilter/master) H:00fc46c3372592059ff63bc0dfb610d8e583fa3e - T:0258b53a79efebb3a1a18fae2f5f2b26338bfaf8
|/  
* () H:19f6a7da7e8d9ca30a644872167bd7f17c3b5f92 - T:6f3003a0d9bd438abda8b48741b0b0a68925dcfc

原仓库的对应日志:

$ cd ../test_repo
$ git log --graph --all  --pretty=format:'(%D) H:%H - T:%T' 
* (HEAD -> master) H:00fc46c3372592059ff63bc0dfb610d8e583fa3e - T:179af78895dc9c453df9caa00ed83b7c5bb9a378
* () H:19f6a7da7e8d9ca30a644872167bd7f17c3b5f92 - T:6f3003a0d9bd438abda8b48741b0b0a68925dcfc

可见提交哈希00fc46c3372592059ff63bc0dfb610d8e583fa3e在两个仓库中对应完全不同的树哈希,且git fsck未检测到任何错误,排除filter-repo损坏数据的可能。

附加验证(git cat-file输出对比)

原仓库中该提交的内容:

$ git cat-file -p 00fc46c3372592059ff63bc0dfb610d8e583fa3e
tree 179af78895dc9c453df9caa00ed83b7c5bb9a378
parent 19f6a7da7e8d9ca30a644872167bd7f17c3b5f92
author REDACTED 1678356278 +0000
committer REDACTED 1678356278 +0000

second change

改写后仓库中该提交的内容:

$ git cat-file -p 00fc46c3372592059ff63bc0dfb610d8e583fa3e
tree 0258b53a79efebb3a1a18fae2f5f2b26338bfaf8
parent 19f6a7da7e8d9ca30a644872167bd7f17c3b5f92
author REDACTED 1678356278 +0000
committer REDACTED 1678356278 +0000

second change

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 16:23:07