I am trying to move files from one local git repository to another local git repository for a different project while preserving history from the original repository. So far I have this, which is working fine if the file was never moved or renamed in the source repo:
# Executed from a directory in the target repository
( cd $SOURCE_REPOSITORY_DIRECTORY && git format-patch -B -M --stdout --root $SOURCE_FILENAME) | git am --committer-date-is-author-date
This happens to work because the directory structures of the two repositories are the same. If they were different, I'd have to create patch files and fix up the directory names using sed
or something.
Anyway, this is all swell until I hit a file that has been renamed. Even though I'm specifying the -B -M
(and get the same results with -B -M -C --find-copies-harder
) I do not get the patches from before the move, even though the file was cleanly moved (similarity index 100%).
This is particularly odd since git log --follow
shows all the commits and git log --follow -p
provides all the diffs. Except it provides them in reverse order so I cannot feed them into git am
.
Note also that git log --follow -p filename
puts out the following "patch" to show the rename:
diff --git a/old_dir_name/dir1/dir2/filename b/new_dir_name/dir0/dir1/dir2/filename
similarity index 100%
rename from old_dir_name/dir1/dir2/filename
rename to new_dir_name/dir0/dir1/dir2/filename
Now if git log
would display the patches in the right format and right order for git am
to apply them, I could just use that, but such is not the case. Using git log --reverse --follow -p filename
only outputs the name change patch, nothing else.
So, how do I get git format-patch
to really follow renames the way the help file/man page says it should while at the same time only outputting patches for a single file? Alternately, how do I get git log -p
to produce patches in a way I can feed them into git am
to recreate a file with history?
I'm using git version 1.8.4.3.