Paweł Poręba Paweł Poręba - 27 days ago 7
Git Question

replay changes ammended to commit on another branch

I have:

1) created

branch1


2) committed
commitA1
locally on
branch1


3) checkout to
branch2
(
commitA1
is applied)

4) Now, on
branch2
I've made some other commits (
commitB1
,
commitB2
, etc.), so it looks like:

----commitB2-----
----commitB1-----
----commitA1-----


5) Then, I've returned to
branch1
cause I needed to change some things. I've ammended those changes to
commit1A


6) I realised that I need those recent ammended changes also on
branch2
.

Here, my question is: how to apply those changes to
branch2
in the fastest way? I want to save existing hierarchy, so I want this changes to be applied to
commitA1
on
branch2
(so before
commitB1
and
commitB2
).

I thought that I could
cherry-pick
, but it gives merge conflicts, and after resolving it looks like:

----commitA1-----
----commitB2-----
----commitB1-----
----commitA1-----


Thanks.

Answer
  1. checkout branch2 and git reset --hard HEAD^ to undo the cherry-pick.
  2. git format-patch branch1^..branch1 --stdout > patch to make a patch that applies changes of the amended commit1A.
  3. git rebase -i HEAD~4, which opens an editor with contents like below:

    pick ed4dcf2 foo
    pick 6af0cca commitA1
    pick e9cfa08 commitB1
    pick d9856c4 commitB2
    (snip)
    
  4. Edit the contents like below:

    e ed4dcf2 foo
    d 6af0cca commitA1
    pick e9cfa08 commitB1
    pick d9856c4 commitB2
    

    This means will edit after the commit foo and drop commitA1

  5. Save and close the editor, which starts the rebase.
  6. The rebase stops after foo. Then, apply the patch by git apply patch, delete the patch file, git add the changes, and git commit them (without --amend).
  7. git rebase --continue to resume the rebase, which applies commitB1 and commitB2.
Comments