我有以下外部git repo:
test % git log
commit 022c4bebe111329c9b07e714c353d68d238d6187
Author: Andreas Pardeike <andreas.pardeike@polisen.se>
Date: Wed Mar 29 10:13:36 2023 +0200
first commit
test % cat README.md
# test
我将其克隆到临时存储库test-1
中并运行
test-1 % git filter-branch --tree-filter '
export GIT_AUTHOR_DATE="$(git log -1 --format=%aI)"
export GIT_COMMITTER_DATE="$(git log -1 --format=%cI)"
echo "// modified" >> README.md
' -- --all
test-1 % git log
commit c79e6cc1180be936358a41d8beaba99a7ce33c71 (HEAD -> main, origin/main, origin/HEAD)
Author: Andreas Pardeike <andreas.pardeike@polisen.se>
Date: Wed Mar 29 10:13:36 2023 +0200
first commit
现在我在原始repo中更改README.md
:
test % echo "# test-new" > README.md
test % cat README.md
# test-new
test % git add .
test % git commit -m "change"
[main f04f865] change
1 file changed, 1 insertion(+), 1 deletion(-)
test % git push
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Writing objects: 100% (3/3), 255 bytes | 255.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/pardeike/test.git
022c4be..f04f865 main -> main
test % git log
commit f04f865c6e546318df7256e4cd48b3bb9505a710 (HEAD -> main, origin/main)
Author: Andreas Pardeike <andreas.pardeike@polisen.se>
Date: Wed Mar 29 10:20:06 2023 +0200
change
commit 022c4bebe111329c9b07e714c353d68d238d6187
Author: Andreas Pardeike <andreas.pardeike@polisen.se>
Date: Wed Mar 29 10:13:36 2023 +0200
first commit
然后我将它克隆到一个新的临时存储库test-2
中,并运行相同的修改:
test-2 % git log
commit f04f865c6e546318df7256e4cd48b3bb9505a710 (HEAD -> main, origin/main, origin/HEAD)
Author: Andreas Pardeike <andreas.pardeike@polisen.se>
Date: Wed Mar 29 10:20:06 2023 +0200
change
commit 022c4bebe111329c9b07e714c353d68d238d6187
Author: Andreas Pardeike <andreas.pardeike@polisen.se>
Date: Wed Mar 29 10:13:36 2023 +0200
first commit
test-2 % git filter-branch --tree-filter '
export GIT_AUTHOR_DATE="$(git log -1 --format=%aI)"
export GIT_COMMITTER_DATE="$(git log -1 --format=%cI)"
echo "// modified" >> README.md
' -- --all
test-2 % git log
commit 00513669b9a95db42317c6734152e7b2861b89b7 (HEAD -> main, origin/main, origin/HEAD)
Author: Andreas Pardeike <andreas.pardeike@polisen.se>
Date: Wed Mar 29 10:20:06 2023 +0200
change
commit 6bc296719d3aa3844c1ebb352f3daf67184f0025
Author: Andreas Pardeike <andreas.pardeike@polisen.se>
Date: Wed Mar 29 10:20:06 2023 +0200
first commit
**问题:**为什么临时仓库test-1和test-2的'first commit'的提交哈希不同?我知道它们与原始的不同,但如何使它们在任何临时仓库中保持相同?
**原因:**我的任务是重写外部仓库,以镜像它们供我们无法访问互联网的内部使用。在镜像过程中,我需要重写一个特定的文件,以将任何依赖URL指向我们的内部git仓库。我通过在所有仓库中处理所有标签/提交的现有已知文件的末尾附加一个静态文本片段来实现这一点。由于这些内部仓库是只读的,因此没有冲突的风险。
或者:当从原始仓库镜像到内部仓库时,我如何“处理新提交”?
2条答案
按热度按时间camsedfj1#
在第二个repo中,both commits' author dates(和假定他们的提交者日期)等于原始仓库中的第二次提交。这表明你
export
的值是错误的。如果你想引用当前处理的提交,你可以做git log -1 ... $GIT_COMMIT
。然而,--tree-filter
默认情况下不会更新这些日期,因此在您的情况下,您不必做任何调整。balp4ylt2#
没关系,
git filter-branch --tree-filter
用它自己的提交哈希创建了一个稳定的新历史,但这些哈希在原始存储库中重复镜像新提交时保持不变。所以只需要使用
git filter-branch --tree-filter 'echo "// modified" >> README.md' -- --all
生成的临时存储库可以无冲突地推送到内部存储库,而不会有冲突的风险。