2

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.

3 Answers 3

2
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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.