Git rebase multiple commits in one branch to the master

Viewed 217

When I read the rebase command in git documentation, I have a question. Is it possible to rebase the three commit x x x in topic 'next' to master?

  o---o---o---o---o  master
           \
            o---o---x---x---x  next

rebase to

  o---o---o---o---o---x---x---x  master
           \
            o---o  next
2 Answers

You really need two steps here. First do a rebase --onto to bring the top 3 commits from next onto master:

master:  o1 -- o2 -- o3 -- o4 -- o5
                       \
next:                   o6 -- o7 -- x1 -- x2 -- x3

Use:

git rebase --onto o5 o7

Then, remove the top 3 commits from the next branch:

# from next
git reset --hard HEAD~3

for this purpose I would look at

git cherry-pick for the new commits on master

and

git reset --hard for clearing those commits on next

a script might look like this:

git switch master
git cherry-pick next~2 next~1 next~0
git switch next
git reset --hard HEAD~3
Related