Last Updated: February 25, 2016
·
1.067K
· butenas_com

Git cherry picking: move small code patches across branches

If you have branches in your repos and need to apply one small code change to all of them and don't want to merge branches... you are lucky!

Git has a nice way to pick those cherries :) cherry-pick is the command you are looking for. Here is how you can pick the cherries.

  • Get the branch you want to work on:
    $ git checkout awesome_branch

  • Do the fix (this is the most important thing here).

  • Commit the fix and get the commit id (remember it... ok ok , copy that):
    $ git commit -a -m "your awesome fix" $ git log

  • Checkout another branch which requires the same changes:
    $ git checkout another_branch

  • Pick the cherries (the commit id you've remebered plays the role here)!
    $ git cherry-pick 579b0b105cd779c41eab63d84af944a1528a2c88

  • Here you are, you have applied the same fix for the another_branch :) Don't trust my word and check:
    $ git log

You will see different commit id hash there, but the changes were applied.