Orion42 Blog

~IT made easy~

Computer and Coding

[Git] How to rebase last commit to a different branch with cherry-pick

In git is possible to rebase the last commit of Branch B onto Branch A and rollback Branch Bone commit using the cherry-pick instruction.

Initial state:

-- -- -- --    (Branch-A)
    \
      -- XX      (Branch-B)

Final state:

-- -- -- -- XX  (Branch-A)
   \
      --         (Branch-B)

You can select and copy (cherry-pick) a specific commit and apply it to the branch you are in (including the ‘master’ one). In our example we then checkout to the Branch A:

git checkout Branch-A
git cherry-pick <hash of XX>

where you have to replace <hash of XX> with the actual hash of XX obtained for example via git log.

Then we can remove the last commit from Branch-B with the git reset instruction.

git checkout Feature-branch
git reset --hard HEAD^

Acknowledgement:

This article was inspired by a real-life problem that was initially solved using this post in StackOverflow

Leave a Reply