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
2️⃣ View commit history of source branch:
git log B --oneline
3️⃣ Copy the commit hash you want to merge.
4️⃣ Apply the commit to your target branch:
git cherry-pick <commit-hash>
5️⃣ Want multiple commits? No problem!
git cherry-pick <commit-hash1> <commit-hash2>
6️⃣ If conflicts occur:
# Resolve conflicts
git add .
git cherry-pick --continue
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! 👇
Author Of article : Dhanasai Tholeti Read full article