Specify Target File in git cherry-pick

Cherry-picking with git could easily become a nightmare if some files are renamed or the directory structure of the project is changed. In these cases, it is often helpful to cherry-pick the problematic files individually.

Fortunately, this can be done in a few easy steps:

  1. Create a patch file for that individual file: git show [commit hash] -- path/to/old/file > patchfile
  2. Manually edit the newly created patchfile and replace all occurrences of the old path with the new path.
  3. Apply the patch via git apply --3way patchfile

The --3way argument is very important and would tell git to do a 3-way comparison. After step 3, if git is unable to figure out how to merge the changes, it will add conflict markers to the resulting file and would allow one to manually resolve the conflicts.

The procedure above can be repeated for every individual file.

Leave a Reply

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.