Notice: Undefined index: aiosp_license_key in /home/gpimxbfj/public_html/wp-content/plugins/all-in-one-seo-pack/aioseop_class.php on line 5808
[Git] How to rebase last commit to a different branch with cherry-pick | Orion42 ~IT made easy~

Orion42 ~IT made easy~

Solving common problem with out-of the box thinking

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

Skip to content