Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rebase last x commits on different branch?

Tags:

git

I want to take the latest X commits from a branch and be able to apply them on top of different branch. For example doing git rebase -i HEAD~10 would allow me to interactively modify and apply the last 10 commits on the same branch I'm on.

Is it possibly to do the same thing but apply the commits on a particularly other branch, where the history might have diverged a lot?

like image 598
ddinchev Avatar asked May 01 '16 14:05

ddinchev


People also ask

How do I rebase a commit to another branch?

To rebase, make sure you have all the commits you want in the rebase in your master branch. Check out the branch you want to rebase and type git rebase master (where master is the branch you want to rebase on).

Can I rebase multiple branches?

The key is to rebase from each branch point in the old tree (e.g. c2 in the above) on to the new tree (e.g. c2' in the above). There's a response submitted here as an answer suggesting the --onto isn't needed on the first or last lines...do you have an opinion and want to respond to their answer or edit yours?


1 Answers

You can use the --onto flag.

git rebase -i HEAD~10 --onto another_branch 

Note that this will not create a new branch, nor will it move the actual changes to another_branch.

All changes will be applied to the same branch you are on.

So I suggest do it in several stages:

git checkout -b staging_branch git rebase -i HEAD~10 --onto another_branch git checkout another_branch git merge staging_branch 
like image 169
Igal S. Avatar answered Sep 23 '22 10:09

Igal S.