DEV Community

Dhanasai Tholeti
Dhanasai Tholeti

Posted on

2 1 1 1 1

πŸš€ 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! πŸ‘‡

AWS Q Developer image

Your AI Code Assistant

Generate and update README files, create data-flow diagrams, and keep your project fully documented. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free β†’