How to Git Rebase a Branch with the onto Command?

I have noticed that the two blocks of following git commands have different behaviours and I don't understand why.

I have an A and a B branches that diverge with one commit

---COMMIT--- (A)
\
 --- (B)

I want to rebase B branch on the lastest A (and have the commit on the B branch)

---COMMIT--- (A)
         \
          --- (B)

No problem if I do:

checkout B
rebase A

But if I do:

checkout B
rebase --onto B A

It doesn't work at all, nothing happens. I don't understand why the two behaviours are different.

PhpStorm GIT client uses the second syntax, and so seems to be completely broken, that's why I ask for this syntax issue.

3

8 Answers

tl;dr

The correct syntax to rebase B on top of A using git rebase --onto in your case is:

git checkout B
git rebase --onto A B^

or rebase B on top of A starting from the commit that is the parent of B referenced with B^ or B~1.

If you're interested in the difference between git rebase <branch> and git rebase --onto <branch> read on.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.