I have the following git repo:
I want to merge C into B while keeping second branch ( C -> D) intact. How can I do that?
I have the following git repo:
I want to merge C into B while keeping second branch ( C -> D) intact. How can I do that?
Merging only some commits from the feature branch into the master branch is known as cherry-pick. Git provides the following command for doing cherry-pick.
$ git cherry-pick COMMIT_ID
In your case, the COMMIT_ID will be the id of the commit C.
In cherry-pick, the data from commit C will be picked(copied) into master. It will not merge but only copy the data and create a new version(commit) in the master.
The easiest way is to checkout
the branch where B
is, and then
git merge HEAD^
If you wanted to skip over some number of commits, you could use ~<n>
syntax instead of ^
; for example
git merge HEAD~5
to omit the most recent 5 commits.
In any case whee you want all the commits "up to" a certain point, this will work and the result will be just like you had made the merge prior to the omitted commits, except that the default commit message will be different.
As others have noted, sometimes it may also make sense not to merge at all, but to integrate the changes either by rebase or cherry-pick. These are both options that create new commits on the target branch, rather than actually merging them in from the source branch. This has implications for future merges between the branches, so unless you're using a merge-less workflow you should exercise caution; but if what you need is not "everything up to commit X", then you may have to go this way. cheery-pick
is good for when you just want one or two (or a few) commits scattered through the branch. Interactive rebase is good if you want most of the branch but need to exclude a couple commits that aren't at the end.