Back to Blog
Lesson 43 of the Git & GitHub: Git & GitHub from Zero course
GitAugust 30, 20264 min read

Cherry-Picking Changes: How to Move Specific Commits in Git

Learn how to use git cherry-pick to move specific commits between branches. Master this essential tool to apply hotfixes and manage your repository history.

GitVersion ControlWorkflowCherry-PickCLI
A farmer picking ripe cherries from a tree in a sunny orchard in British Columbia, Canada.

Previously in this course, we explored Advanced Branching Patterns: Mastering Release Branches and Hotfixes, where we discussed isolating critical production fixes. Sometimes, you need to apply a fix or a feature from one branch to another without bringing along all the other changes from that branch. That is where git cherry-pick comes in.

Understanding Cherry-Picking from First Principles

In Git, a commit is a snapshot of your project at a specific point in time, identified by a unique hash. Usually, we integrate work using Creating Pull Requests: Proposing Changes in Git & GitHub or a standard merge. These methods combine the entire history of two branches.

git cherry-pick is different. It allows you to select one or more specific commits by their hash and apply them as new commits onto your current branch. It essentially says to Git: "Take the changes introduced in this specific commit and replay them here."

When to Use Cherry-Picking

  • Applying a Hotfix: You patch a bug on your develop branch but need that fix on main immediately, without merging all the other unfinished work in develop.
  • Porting Features: You have a small feature in a experimental branch that you want to move to your stable branch without including other experimental commits.
  • Recovering Lost Work: You accidentally committed to the wrong branch and want to "move" the commit to the right one.

Worked Example: Applying a Specific Fix

Imagine you are working on our course project. You have a feature-login branch where you fixed a typo in the README.md, but you also have several other work-in-progress commits in that branch that aren't ready for main.

  1. Identify the target commit: First, check the log of the source branch to find the hash of the commit you want to move.

    Bash
    git checkout feature-login
    git log --oneline
    # Output:
    # a1b2c3d (HEAD -> feature-login) Fix typo in README
    # e4f5g6h Add login validation logic
    # i7j8k9l Initial setup
  2. Switch to the destination branch: Move to the branch where you want the change to land (e.g., main).

    Bash
    git switch main
  3. Cherry-pick the commit: Use the hash you identified:

    Bash
    git cherry-pick a1b2c3d

Git will create a new commit on your main branch containing the exact changes from a1b2c3d. Note that because it creates a new commit, the hash will be different, even though the content is the same.

Hands-on Exercise

  1. Switch to your project's main branch.
  2. Create a new, temporary branch called demo-cherry-pick.
  3. Make a small change to a file, commit it, and note the commit hash using git log.
  4. Switch back to main.
  5. Run git cherry-pick <hash> to bring that change over.
  6. Verify the file content is updated on main.

Common Pitfalls

  • Merge Conflicts: If the code you are trying to cherry-pick clashes with the current state of your target branch, Git will pause and ask you to resolve the conflict. You'll need to edit the files, run git add <file>, and then git cherry-pick --continue.
  • Duplication: Cherry-picking creates a new commit. If you later merge the source branch into the target branch, Git might get confused or create redundant commits because it sees two different commits (with different hashes) that contain the same changes.
  • The "Why" Factor: Because cherry-picking is a "surgical" operation, it can leave your history looking a bit disjointed. Always leave a clear message in your commit or PR explaining why you chose to cherry-pick a change rather than performing a standard merge.

FAQ

Does cherry-picking remove the original commit? No. The original commit remains exactly where it was in the source branch.

Can I cherry-pick multiple commits at once? Yes. You can provide multiple hashes separated by spaces: git cherry-pick <hash1> <hash2>.

What if I want to abort a cherry-pick? If you run into a conflict you aren't ready to resolve, simply run git cherry-pick --abort to return the repository to the state it was in before you started the operation.

Recap

git cherry-pick is a powerful surgical tool. It allows you to move specific commits between branches by creating a new, identical commit in the target branch. Use it to keep your production branches stable without pulling in incomplete features or experimental code.

Up next: We will explore the trade-offs between linear history and merging, as we dive into Rebasing vs Merging.

Similar Posts