daredevil1234
daredevil1234

Reputation: 1425

Git restructure commits

I have the following scenario

Branch A: c1, c2
Branch B: c1, c2, c3, c4
Branch D: c1, c2, c3, c4, c5, c6

Now, branch D was meant to branch from Branch A, but I mistakenly branched from branch B. The commits, c5 and c6 are not related to commits, C3 and c4.

How do I go about making a new Branch, E, from Branch A, and moving just commits C5 and C6 over to this new branch such that it is structured like this:

Branch E: c1, c2, c5, c6

Upvotes: 0

Views: 1196

Answers (1)

combinatorist
combinatorist

Reputation: 566

Option 1:

If you really only have 2 commits to move you can do:

git checkout -b E A
git cherry-pick c5
git cherry-pick c6

Option 2:

But git rebase let's you do multiple commits at a time:

gut checkout -b E D
git rebase --onto A B

First, we start a new branch E at D. Then the rebase syntax is to take everything in E "since B" and recommit it "onto" A.

Variations / Options

  • You can use the -i flag for interactive and you should only see commits c5 and c6 in the "todo".
  • Technically, the last argument of the rebase defaults to the branch you're in, but you could explicitly call it out (E), which helps if you are not already on it.

Upvotes: 2

Related Questions