git cherry-pick with target file renamed

前端 未结 2 937
死守一世寂寞
死守一世寂寞 2021-01-17 10:33

I have two branches for different versions - one for current development and another is long-time support several years old. As a consequence they differ significantly. I ha

2条回答
  •  暖寄归人
    2021-01-17 11:06

    The cherry-pick command is using the same merge strategies as the merge command. The default recursive merge strategy is able to detect renames under normal circumstances, but it can fail if the files have diverged too much like in this case.

    Apart from the low level index hacking outlined in the other answer, it might be possible to make it work simply by fiddling with the strategy options, for example increase the threshold for considering a similar file to be a renamed version (default is 50%):

    git cherry-pick -Xrename-threshold=20% 0123sourcecommithash
    

    Otherwise you could also always do it the manual way, i.e.

    git format-patch 0123sourcecommithash -1
    

    Then edit the created 0001-Commit-message-here.patch to change the file path and apply again on the target branch:

    git am 0001-Commit-message-here.patch
    

    It will retain all original commit messages, author names, dates etc. and might work as an easy one shot solution.

提交回复
热议问题