Git: How to rebase to a specific commit?

I'd like to rebase to a specific commit, not to a HEAD of the other branch:

A --- B --- C          master
 \
  \-- D                topic

to

A --- B --- C          master
       \
        \-- D          topic

instead of

A --- B --- C          master
             \
              \-- D    topic

How can I achieve that?


Solution 1:

You can avoid using the --onto parameter by making a temp branch on the commit you like and then use rebase in its simple form:

git branch temp master^
git checkout topic
git rebase temp
git branch -d temp

Solution 2:

You can even take a direct approach:

git checkout topic
git rebase <commitB>

Solution 3:

Use the "onto" option:

git rebase --onto master^ D^ D