【发布时间】:2015-03-03 11:40:55
【问题描述】:
问题
我想将一个文件夹(以及包含子文件夹的文件)从一个存储库移动到另一个存储库,同时保留历史记录。
我在 SE 上找到了一种方法:How to move files from one git repo to another (not a clone), preserving history。 在blog.neutrino.es 上有不同的想法。这是我想在这里讨论的最后一个。
尝试的解决方案
mkdir /tmp/mergepatchs
cd ~/repo/org
export reposrc=myfile.c #or mydir
git format-patch -o /tmp/mergepatchs $(git log $reposrc|grep ^commit|tail -1|awk '{print $2}')^..HEAD $reposrc
cd ~/repo/dest
git am /tmp/mergepatchs/*.patch
如果我理解正确,这个想法是假装我们将通过电子邮件提交提交,然后将它们重新导入另一个存储库。
错误
我在执行git am /tmp/mergepatchs/*.patch 时收到此错误消息:
Applying: Initial commit
error: .gitignore: already exists in index
error: README.md: already exists in index
Patch failed at 0001 Initial commit
The copy of the patch that failed is found in:
/Users/myuser/repo/org/.git/rebase-apply/patch
When you have resolved this problem, run "git am --continue".
If you prefer to skip this patch, run "git am --skip" instead.
To restore the original branch and stop patching, run "git am --abort".
为了更好地理解这个过程,我先尝试了一个文件(而不是整个目录)。然而,git am 之后“什么都没有”发生(即没有新文件,git status 没有报告任何变化)。这是为什么呢?
然后我尝试了:
INITCOMMIT=$(git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$")
git format-patch -1 -o /tmp/mergepatchs ${INITCOMMIT}
但随后收到与之前相同的错误消息。
为什么补丁失败了?
编辑 1
我尝试了一些相关的东西,灵感来自How to create and apply a patch with Git。
在~/repo/org:
$ git format-patch --root HEAD --stdout myfile.c > /tmp/mergepaths/01.patch
在~/depo/dest:
$ git apply --stat /tmp/mergepaths/01.patch
0 files changed
$ git apply --check /tmp/mergepaths/01.patch
$ git am < /tmp/mergepaths/01.patch
stat 和 check 都告诉我“什么都不做”。补丁对象远非空。
顺便说一句,我不知道这是否相关,但补丁的创建和应用都是在分支中完成的。
【问题讨论】:
标签: git