0

I often have to git cherry-pick from an old project to a new project. The new project hosts all files in a /server/ subdirectory. This throws off all the cherry-picks as it thinks the files should be, e.g. /path/to/project/myfile.php rather than /path/to/project/server/myfile.php

Is there a way to resolve this with Git?

Many thanks in advance.

a.ak
  • 659
  • 2
  • 12
  • 26

2 Answers2

1

Because a cherry-pick is a merge, Git will sometimes figure this out on its own. If Git does not figure this out on its own, you can try fiddling with the rename-detection threshold level: git cherry-pick -X find-renames=value. The value can be expressed as a percentage, with a literal percent character, e.g., 50% (the default), 75% (tighten rename finding if it's finding renames that don't actually make sense), 25% (loosen rename finding if it's failing to find renames), and so on. Or, as long as the value is expressed as exactly two digits, you can leave off the % character:

git cherry-pick -X find-renames=25 <hash>

If your Git is very old, find-renames may be spelled rename-threshold instead. Consult your installed Git documentation, specifically that for git merge.

If lowering the rename threshold all the way (to 01; zero just means "disabled" so does not work) does not help, there is unfortunately no easy way to deal with this. You can turn the commit into a patch—e.g., with git format-patch—and edit the path name in the resulting patch, though.

torek
  • 448,244
  • 59
  • 642
  • 775
  • I havent checked if it works, but the `ort` `subtree` option could be used instead of `find-renames`, like `git cherry-pick -X subtree=server ` in the question's scenario – forivall Mar 14 '23 at 18:24
0

In case the git root of the old project is in /path/to/project_old and the new project has its git root in /path/to/project_new/server, cherry-picking from one project to the other is easy:

Assuming both are local paths, you can add /path/to/project_old as a remote to /path/to/project_new/server (or directly clone from it) and do all the git commands as if /path/to/project_old would be a git remote server:

Hope this explains the idea:

cd /path/to/project_new
git clone /path/to/project_old server
cd server
git cherry-pick <commit-hash>
orzechow
  • 1,210
  • 2
  • 13
  • 18