将文件从一个回购的特定 commit/hash 复制到另一个(不相关的)回购

Copying a file from a specific commit/hash from one repo into another (unrelated) repo

我有两个不相关的回购协议,AB。我希望从 A 获取特定 commit/hash/tag 的特定文件并将其复制到 B.

我在本地有 AB,即都是克隆的。

到目前为止,我唯一的方法是转到 A,签出所需的提交,返回到 B 并执行 cp path_to_repo_A/file path_to_repo_B/file

您可以在 repo A 中使用 git show "[commit/hash/tag]:[file]" 来打印 git 中的文件内容,而无需将其签出。然后您可以将其传递给存储库 B 中的文件。

例如在 main 分支上复制 README.md

cd A
git show "main:README.md" > ../B/README.md
cd ../B
git add README.md
git commit

您可能还会发现 git 的 -C 选项很有用 - 它让您可以指定 git 的工作路径,而不必 cd 到它:

cd B
git -C ../A show "main:README.md" > README.md
git add README.md
git commit