如何将本地未推送的提交从损坏的 git 存储库移动到另一个存储库?

How do I move local, unpushed, commits from a corrupt git repository to another repository?

通常我可以使用 cherry-pick 将推送的提交移动到另一个存储库:我 fetch 相关分支从 'another' 存储库到我的新存储库和 cherry-pick 带有提交 ID .

我想对损坏的存储库中的 local--unpushed--commits 做同样的事情(出了点问题,我无法推送我的更改,但我需要将它们移动到另一个存储库)。

有什么方法可以 cherry-pick 从损坏的仓库提交到另一个仓库?

PS:两个存储库都存在于同一台机器上。

您是否只需要将提交从一个本地存储库推送到另一个本地存储库?

git remote add local /path/to/repo.git
git push local master

远程不限于另一台机器:如果您在 /repo1 有一个存储库,在 /repo2 有另一个存储库,您可以通过以下方式将提交从 repo1 复制到 repo2使用文件系统路径简单地将 repo1 作为远程添加到 repo2

cd /repo2
git remote add repo1 /repo1

您现在可以 fetchpullcherry-pickrepo1 提交到 repo2

另一种可能的方法。

cd repo1_path
git format-patch -1 <commit> --stdout > /tmp/xxx.patch
cd repo2_path
git checkout <branch>
git am /tmp/xxx.patch