WoLfPwNeR
WoLfPwNeR

Reputation: 1324

Git: how to rebase onto previous commit

I'm working on branch C which is based on branch B, then branch A:

A---B---C

Is there any commands that can make branch C directly based on branch A like this:

A---B \ --C

I have tried git rebase A but it does not work.

Upvotes: 2

Views: 750

Answers (3)

Adrian Shum
Adrian Shum

Reputation: 40066

git rebase --onto A B C

Explanation:

In rebase command, you can define:

  1. Branch to rebase (by default = HEAD)
  2. The upstream branch of branch-to-rebase
  3. Target Branch to rebase to (by default = upstream branch)

and the command is

git rebase --onto TargetBranchToRebaseTo UpstreamBranch BranchToRebase

In fact you can almost an exact example in git help rebase

Upvotes: 2

bk2204
bk2204

Reputation: 76904

Git has a builtin option to rebase that does just that:

git checkout C
git rebase --onto A B

This rebases all of the commits after B up to C such that they start on A instead of B.

Upvotes: 0

Jonah Bishop
Jonah Bishop

Reputation: 12591

Here's what I would do:

git checkout C
git checkout -b rebaseTemp
git rebase -i A

In the interactive rebase, delete all of the commits that correspond to branch B, but aren't a part of branch C (i.e. the commits that are shared between the two). Complete the rebase, then you will have a new branch (rebaseTemp) that contains what I think you want. You can then merge A into rebaseTemp (which will result in a fast-forward merge).

A branch visualization tool (i.e. gitk) is really useful for this scenario, so you can see what commits are shared.

Upvotes: 0

Related Questions