Problem
I'd like to move a folder (and subfolder contained files) from one repository to another, preserving the history.
I found one approach on SE: How to move files from one git repo to another (not a clone), preserving history. And a different idea on blog.neutrino.es. It is the last one that I'd like to discuss here.
Attempted Solution
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
If I understand correctly, the idea is to pretend we're going to submit the commits by e-mail, and re-import them in another repository.
Error
I get this error message when doing the 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".
In order to understand better the process, I tried a single file first (instead of the whole directory). However "nothing" happened after git am
(i.e. no new file, git status
does not report any change). Why is that?
I then tried:
INITCOMMIT=$(git rev-list --parents HEAD | egrep "^[a-f0-9]{40}$")
git format-patch -1 -o /tmp/mergepatchs ${INITCOMMIT}
But then got the same error message as before.
Why are the patches failing?
Edit 1
I tried something related, inspired from How to create and apply a patch with Git.
In ~/repo/org
:
$ git format-patch --root HEAD --stdout myfile.c > /tmp/mergepaths/01.patch
In ~/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
Both stat
and check
tell me that "nothing" will be done. The patch object is far from empty.
By the way, I don't know if this is relevant but both the creation and application of patches are done in branches.