Skip to main content

Command Palette

Search for a command to run...

Git Cherry-pick

Published
3 min read
Git Cherry-pick

The "Copy-Paste" Button for Your Commits

Have you ever made a perfect bug fix on a "messy" experimental branch, and realized you need that fix on your main branch right now?

You can’t merge the whole branch because it’s full of unfinished code. You just want that one specific commit.

This is exactly what Git Cherry-pick is for.

In this guide, I’ll explain how to use it in plain English. No complex jargon, just the steps you need to get the job done.

What is Cherry-picking?

Imagine you have a music playlist (Branch A) with 50 songs. You want one specific song from that playlist to be on your Party Playlist (Branch B).

You don't want to copy the whole playlist. You just want to pick that one song and drop it into the new list.

Git Cherry-pick does exactly that. It takes a specific commit from one branch and copies it to another branch.

How to Do It (3 Simple Steps)

Let's say you have a commit on a branch called feature-login that fixes a crash, and you want that fix on main.

Step 1: Find the Commit Hash

First, go to the branch that has the commit you want. You need the Commit Hash (the ID number of the commit).

Run this command:

Bash

git log --oneline

You will see a list like this:

  • a1b2c3d - Added cool styling

  • e9f8g7h - FIX: Fixed login crash <-- This is the one we want!

  • j5k4l3m - Initial commit

Copy that code: e9f8g7h.

Step 2: Switch to the Target Branch

Now, go to the branch where you want to paste the commit (e.g., main).

Bash

git checkout main

Step 3: Cherry-pick It!

Run the command using the ID you copied earlier:

Bash

git cherry-pick e9f8g7h

That’s it! Git just took the changes from that one commit and applied them to your main branch.


When Should You Use This?

Here are the most common real-world scenarios:

  1. The "Hotfix" Situation: You fixed a critical bug on your development branch, but you need that fix on production immediately without releasing all the other unfinished features.

  2. The "Wrong Branch" Mistake: You accidentally committed a bug fix to the wrong branch. You can cherry-pick it to the right branch and then delete it from the wrong one.

  3. Team Collaboration: A teammate wrote a helper function on their branch that you need right now on your branch.

What If It Gets Stuck? (Conflicts)

Sometimes, just like a Merge, a Cherry-pick can run into a Conflict. This happens if the file you are changing is totally different on the two branches.

If Git stops and says "CONFLICT," don't panic.

  1. Open the files with conflicts and fix the code (VS Code makes this easy).

  2. Add the files:

    Bash

     git add filename.js
    
  3. Tell Git to continue:

    Bash

     git cherry-pick --continue
    

(If you realize you made a mistake and just want to stop, type git cherry-pick --abort to go back to normal).

Quick Cheatsheet

Goal

Command

Copy one commit

git cherry-pick <commit-id>

Copy multiple commits

git cherry-pick <id-1> <id-2>

Continue after fixing conflict

git cherry-pick --continue

Cancel/Stop the process

git cherry-pick --abort

Final Thoughts

Git Cherry-pick is a superpower for developers. It gives you surgical precision to move code around without making a mess. Next time you need just one specific change from another branch, don't merge—cherry-pick it!