How to apply a Git patch to a file with a different name and path?

后端 未结 6 1063
终归单人心
终归单人心 2020-12-04 09:10

I have two repositories. In one, I make changes to file ./hello.test. I commit the changes and create a patch from that commit with git format-patch -1 HE

6条回答
  •  忘掉有多难
    2020-12-04 09:17

    Building upon the answer by @georgebrock, here's a solution I used:

    First, create the patch files as usual (eg. git format-patch commitA..commitB).

    Then make sure that your target repository is clean (there should be no changed or untracked files) and apply the patches like this:

    cd second-repo
    git am ~/00*.patch
    

    For every patch file you will get an error like "error: XYZ does not exist in index". You can now apply this patch file manually:

    patch --directory blue/red < ~/0001-*.patch
    git add -a
    git am --continue
    

    You have to do these three steps for each patch file.

    This will preserve the original commit message etc. without requiring any special git format-patch command or editing the patch files.

提交回复
热议问题