DEV Community

Dhanasai Tholeti
Dhanasai Tholeti

Posted on

๐Ÿš€ Git Cherry-Pick: The Hidden Gem You Need to Know! ๐Ÿš€

Last Friday, I made a small typo fix and accidentally pushed it straight to production instead of the testing environment. Since it was already live, I had to merge the change back into testing.

At first, I thought of pulling production into testingโ€”but that felt unnecessary. Then, while digging through Gitโ€™s documentation, I found a game-changing command: git cherry-pick.

๐Ÿ’ What is Git Cherry-Pick?

It lets you pick and apply specific commits from one branch to another without pulling or rebasing. Neat, right?

Hereโ€™s a quick guide on how to use it:

๐Ÿ”น Scenario: You have two branches, A (target) and B (source), and you need a specific commit from B in A.

๐Ÿ”น Steps:

1๏ธโƒฃ Checkout your target branch:

   git checkout A
Enter fullscreen mode Exit fullscreen mode

2๏ธโƒฃ View commit history of source branch:

   git log B --oneline
Enter fullscreen mode Exit fullscreen mode

3๏ธโƒฃ Copy the commit hash you want to merge.

4๏ธโƒฃ Apply the commit to your target branch:

   git cherry-pick <commit-hash>
Enter fullscreen mode Exit fullscreen mode

5๏ธโƒฃ Want multiple commits? No problem!

   git cherry-pick <commit-hash1> <commit-hash2>
Enter fullscreen mode Exit fullscreen mode

6๏ธโƒฃ If conflicts occur:

   # Resolve conflicts
   git add .
   git cherry-pick --continue
Enter fullscreen mode Exit fullscreen mode

And just like that, you've merged the changes you needed without messing up the Git history! ๐Ÿ˜Ž

๐Ÿ’ก Have you used git cherry-pick before? Share your experience below! ๐Ÿ‘‡

Top comments (0)