Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git cherry-pick with target file renamed

Tags:

git

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 have some file that exists in both branches, but was renamed in development. One of fixes was made in development branch originally, but later found to be required in support branch as well. But cherry-pick reports that the file does not exist in support branch (because it was renamed in development and in support it has different name).

Contents of this particular file is similar, so it seems patch should be applied fine - if I can direct git what file it should apply it to. Is there a way (preferably in command line) to manually direct git to do what I need?

like image 566
Ivan Danilov Avatar asked Sep 29 '15 12:09

Ivan Danilov


People also ask

How do you commit cherry pick changes?

To change the commit message when cherry-picking, use “git cherry-pick” with the “-e” option. As illustrated in this example, your default editor will open and it will let you change the commit message. When you are satisfied with the edits, save your file and your commit message should be saved successfully.

How do you cherry pick a merged pull request?

You can use the GitLab UI to cherry-pick merge requests into a project, even if the merge request is from a fork: In the merge request's secondary menu, click Commits to display the commit details page. Click on the Options dropdown and select Cherry-pick to show the cherry-pick modal.

Does git cherry pick rewrite history?

And your git history will be rewritten completely, meaning you cannot get back to the state it was before rebasing, like when merging a branch directly. When you merge a branch, your git history looks like this —> your commits + merged branch with all the things.


1 Answers

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.

like image 133
Yirkha Avatar answered Oct 10 '22 05:10

Yirkha