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)