Only move last n commits to other branch

Viewed 75

My structure currently looks like this:

master   A-B-C-D-E-F
                \
                 \
branch2           D2 - D3 - D4

And I want to move the last two commits (D3 and D4) from branch2 to master without moving D2. So it should look like this:

master   A-B-C-D-E-F-D3-D4
                \
                 \
branch2           D2

How can this be achieved?

Please consider that the master-branch already exists - I have already seen several answers merging to new branches but this is not valid here.

1 Answers

To expand on Janez Kuhar's comment, what you'll do is

git checkout master
git cherry-pick D3
git cherry-pick D4

where D3 and D4 are the shas for the commits.

Since we don't want to have duplicate commits and you indicated that you want to remove those commits from branch2, we then do:

git revert D3^..D4

again where D3 and D4 are the shas for the commits.

Related