1

I want to backport a specific commit from a master branch of a project to some older branch that was branched off it. E.g. under "branchA", /my/nice/path/MyClass.java" should receive changes made by a particular commit under "master", to "/my/nice/refactored/file/path/MyClass.java". How do I achieve this "the right way"? (e.g. I can just copy over the changes manually and treat the change in "branchA" as a regular edit, but that doesn't seem right)

In perforce, I could do integrate in a manner that would allow me to specify both paths, including the branches; what's the git equivalent?

Thanks!

Sergey
  • 636
  • 6
  • 12

2 Answers2

0

I think you are looking for git cherry-pick:

Given one or more existing commits, apply the change each one introduces, recording a new commit for each.

That is “the right way”

Chronial
  • 66,706
  • 14
  • 93
  • 99
  • that doesn't work with changed paths. It just creates the files at new paths/deletes the files – Sergey Oct 17 '12 at 23:04
  • ah, didn’t see that. I think if your path changed then there is no “nice” way to do that. See the answer linked in the comment to your question for help on that. – Chronial Oct 18 '12 at 11:11
0

I ended up doing something like this (doesn't handle new files)

COMMIT_TO_PORT=(commit hash)
for f in `git show --pretty="format:" --name-only ${COMMIT_TO_PORT}`
do
PATH_UNDER_MASTER=$f
PATH_UNDER_BRANCHA=(make branchA path out of $f)
FILEID=(optional - extract file name or some other ID to preserve new-old files for reference if auto-generated conflicts are not clear)
git show "${COMMIT_TO_PORT}:{$PATH_UNDER_MASTER}" > "new_${FILEID}"
git show "${COMMIT_TO_PORT}~1:{$PATH_UNDER_MASTER}" > "old_${FILEID}"
git merge-file $PATH_UNDER_BRANCHA "old_${FILEID}" "new_${FILEID}"
# optional - open $PATH_UNDER_BRANCHA 
unset FILEID PATH_UNDER_MASTER PATH_UNDER_BRANCHA
done
unset COMMIT_TO_PORT
Sergey
  • 636
  • 6
  • 12