Sachin Sharma Sachin Sharma - 2 months ago 5x
Git Question

How to pick an amended commit from another branch

I have 2 branches, say "branch_1" and "branch_2" like so:

A <--master
B <--branch_1
C <--branch_2

Now I make a change in branch_1 and amend the HEAD commit. So now the setup looks like:

A <--master
| B_amended <--branch_1
B---C <--branch_2

A git log on branch_1 shows commit B_amended on top of HEAD commit of A. A git log on branch_2 shows commit B and C on top of HEAD commit of A. Commit B on branch_2 does not contain amended changes of commit B_amended on branch_1.

My question is: how do I bring B_amended into branch_2 to make it look like this?

A <--master
B_amended <--branch_1
C <--branch_2

Right now I am doing:

$ git checkout branch_2
$ git reset --soft HEAD~
$ git stash
$ git rebase branch_1
$ git stash apply
$ git commit

Is there a better method?


I did find a better method: Do an interactive rebase and delete commit "B" from "branch_2".

So after "B_amended" in "branch_1", do the following:

$ git checkout branch_2
$ git rebase branch_1 -i

In the editor that pops up, delete the line that says the equivalent of:

pick B

Save and exit

Doing so will essentially replay just "C" on top of "B_amended".