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

后端 未结 6 1046
终归单人心
终归单人心 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:25

    There is a simple solution that does not involve manual patch editing nor external script.

    In the first repository (this may also export a range of commit, use -1 if you want to select only one commit) :

    git format-patch --relative  --stdout > ~/patch
    

    In the second repository :

    git am --directory blue/red/ ~/patch
    

    Instead of using --relative in git format-patch, another solution is to use -p option in git am to strip n directories from the path of the patches, as mentioned in a answer to a similar question.

    It is also possible to run git format-patch --relative without the --stdout, and it will generate a set of .patch files. These files can then be fed directly to git am with git am --directory blue/red/ path/to/*.patch.

提交回复
热议问题