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! ๐
Top comments (0)