I have two repositories with a similar structure, but no common commits. I've added them as remotes of each other to be able to cherry-pick
commits between them.
Recently, I've noticed that git correctly applies the commit's changes even if the files' paths in the repositories differ (and the files themselves differ). How does git
find the file(s) to apply the changes to? Does it look through all the files in the current snapshot?
See phd's suggested duplicate, How do cherry-pick and revert work? as cherry-pick really does use git merge
.
Note that git merge
runs git diff --find-renames
(well, the internal equivalent of it anyway). This is the step that discovers which files are renamed from which other files. To see how that works, see my answers to Trying to understand `git diff` and `git mv` rename detection mechanism and Git Diff of same files in two directories always result in "renamed". The rename-detector is run on both diffs:
git diff parent child
, where child is the commit being cherry-picked and parent is its parent; andgit diff parent HEAD
, which finds the changes on "our" side of the merge operation. Note that parent
here is the parent commit of the commit being cherry-picked, not the parent of HEAD
itself.Typically, the parent-vs-child has few renames to find (most people don't cherry-pick a massive-rename operation), but the parent-vs-HEAD
may have a large number of renames to detect.