Can I merge a branch from the future at a commit in the past without merging the shared commits between them, or without cherry-pick?

StackOverflow https://stackoverflow.com/questions/21313252

Say I have a tag, v1.5. More commits were made and another branch fix was created at a point ahead of tag v1.5.

Now that the fix in fix has been made, it needs to be applied to the released version of the code represented by the tagged commit at 1.5.

Is it possible, without using git-cherry-pick, to merge a large set of commits from fix at a given point in the history?

o  ... (history)
|
o  tag v1.5
|
o (can not have in v1.5)
|
o (can not have in v1.5)
|
o (can not have in v1.5)
|\
| \ (`fix` branch)
|  \
|   o (needed at v1.5)
o   | -(can not have in v1.5)
|   o (needed at v1.5)
|   |
|   o (needed at v1.5)
o   | -(can not have in v1.5)
    o (needed at v1.5)
有帮助吗?

解决方案

Yes, this is possible with rebase instead of cherry-pick. The end result should be the same though regardless of which method is used.

First create a new name for the fix branch so that the original fix branch is left unchanged

git branch fixv1.5 fix

Then rebase fixv1.5 onto v1.5

git rebase --onto v1.5 <upstream> fixv1.5

where <upstream> should be the SHA of the commit that the fix branch was based on, i.e. the one marked P in the diagram below.

Now you should have a history that looks like:

o  ... (history)
|
o  tag v1.5
| \
|  \   A'  B'  C'  D'
|   ---o---o---o---o  fixv1.5
|
o (can not have in v1.5)
|
o (can not have in v1.5)
|
o (can not have in v1.5) P
|\
| \ (`fix` branch)
|  \
|   o (needed at v1.5) A
o   | -(can not have in v1.5)
|   o (needed at v1.5) B
|   |
|   o (needed at v1.5) C
o   | -(can not have in v1.5)
    o (needed at v1.5) D

For completeness the equivalent use of cherry-pick would be

git checkout -b fixv1.5 v1.5
git cherry-pick <upstream>..fix
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top