问题
I have two commits, B and C. C is fixed version of B but not a successor as B was amended. I want to apply this fix to another branch with last commit A. How can I do that, ideally without intermediate patch file?
C
| B
|/
.
. A <-- I want to apply diff from B to C here.
|/
I thought it could be git cherry-pick --no-commit B C but no.
回答1:
One approach would be to rebase onto B, then cherry-pick the resulting C′ (“C prime”) onto A. C′ has only the changes from C that aren't already in B.
Visually:
C′
C |
| B
|/
.
. C″
. |
. A
|/
Commands:
$ git rebase --onto B C^ C
# Fix conflicts that may occur and commit.
$ git tag C-prime
$ git checkout A
$ git cherry-pick C-prime
# Again, fix conflicts because Git can be a little dumb sometimes (by design!)
In the above, I'm just assuming all of the lettered identifiers are tags, not branches, so that my explanation doesn't have identifiers changing what commit they point to. In reality, you likely won't need the git tag … command.
回答2:
Unfortunately, there is no such command out of the box. There are low-level commands like git read-tree -m but they do not provide any usable conflict resolution.
The most practical way is to make a fake commit from B to C and then cherry-pick it. These commands do it:
$ git checkout A
$ git cherry-pick --edit $(echo temporary | git commit-tree -p B C^{tree})
(the way to refer to tree object comes from man gitrevisions)
来源:https://stackoverflow.com/questions/34765177/git-how-to-diff-two-commits-and-apply-that-diff-to-a-third-one