Home Ask Login Register

Developers Planet

Your answer is one click away!

Sam February 2016

Git interactive rebase of another branch on current branch but NOT onto HEAD

I was wondering if there was a way to do an interactive rebase from one branch onto the current branch but not onto the head. Kind of like combination of git rebase -i otherbranch and git rebase -i HEAD~2.

What I want to do is be presented with an interactive rebase file where i can arrange not only the commits from the remote branch but also a range of commits on the current branch beginning from the head.

The scenario I have is:

  • i have feature branch B1
  • while waiting for code review I started a new feature by branching from B1. The new branch is B2
  • code review finally happened and I had to fix something in B1
  • changes are applied to B1 by way of an amend (not a new commit)

Now I need to update B2 with the revised commit from B1. I want the revised the commit to replace the original commit.

Simply rebasing B1 into B2 will result in the original and the revised commit appearing in B2's history (and there will probably be some nasty merge conflicts).

If I could get the B1 unique commits and a range of B2's commits in the interactive rebase prompt then i can tell git to discard the old version of the commit and use the new one from B1 in it's place all in one operation.

So it might look something like: git rebase -i B1 HEAD~2

Currently I do this by branching B1 into temp then cherry-picking the new commit from B2 onto it, erase B2 then move temp to B2. Which is fine I guess... but if i could do it as part of a rebase it would be nicer (i think).


Jokester February 2016

Rebasing is indeed cherry-pick-ing a few commits in turn. The file edited in git rebase -i specifies what commits to pick, and in which order.

We can even replace one commit with another in B1, without a temp branch, like:

# this is the file get edited in `rebase -i`
# pick commit-in-B2        # the default when rebasing B2, comment out it
pick commit-in-B1          # instead pick the amended commit in B1
pick rest-of-B2

Post Status

Asked in February 2016
Viewed 2,595 times
Voted 11
Answered 1 times


Leave an answer

Quote of the day: live life