NeuroAgent

Git Cherry-Pick: Complete Guide to Selective Commits

Learn how to use Git cherry-pick command to selectively apply commits across branches. Complete guide with examples, conflict resolution, and advanced options for precise code integration.

Question

What does cherry-picking a commit with Git mean?

What does the git cherry-pick <commit> command do?

NeuroAgent

Git cherry-pick is a powerful command that allows you to select specific commits from one branch and apply them to another branch without merging the entire branch history. It creates a new commit with the same changes as the original commit but with a different commit hash, enabling you to selectively integrate individual changes across branches.

Contents

Understanding Git Cherry-Pick

The git cherry-pick command is one of Git’s most useful tools for selective commit integration. According to the Atlassian Git Tutorial, it “enables arbitrary Git commits to be picked by reference and appended to the current working HEAD.” This means you can take a commit from anywhere in your repository and apply it to your current branch.

Cherry picking differs significantly from other Git operations like merge or rebase. As DataCamp explains, unlike merge or rebase which work with entire branches, cherry-pick allows you to take specific commits from one branch and apply them individually. This granularity gives developers precise control over which changes they want to integrate.

The Git documentation notes that cherry-picking is typically used when you “cannot cherry-pick a merge because you do not know which side of the merge should be considered the mainline.” This highlights an important limitation and use case consideration when working with merge commits.

How the Command Works

When you execute git cherry-pick <commit>, Git performs several operations:

  1. Identifies the target commit: The command locates the specified commit hash in your Git history
  2. Applies changes: Git applies the changes from that commit to your working directory
  3. Creates new commit: Git creates a new commit with the same changes but a different commit hash
  4. Updates branch: The new commit becomes the HEAD of your current branch

As GitKraken describes it, the command “takes changes from a target commit and places them on the HEAD of the currently checked out branch.” This process effectively “copies” the changes rather than moving the actual commit.

The official Git documentation provides technical details about how the command handles the commit replay process, including options like --mainline for handling merge commits.

Common Use Cases

Cherry-picking serves several important scenarios in Git workflows:

Bug Fixes Across Branches

When you fix a bug in a development branch and need the same fix in a stable release branch, cherry-pick allows you to apply just that specific commit without merging the entire development branch.

Feature Porting

As Stack Overflow explains, a common use is to “forward- or back-port commits from a maintenance branch to a development branch.” This is particularly useful in release management workflows.

Selective Integration

When working with large pull requests, you might want to integrate only specific commits rather than the entire PR. Cherry-pick provides this selective capability.

Emergency Hotfixes

For critical fixes that need immediate deployment, cherry-pick allows you to quickly apply fixes from any branch to your production branch.

Practical Examples

Basic Cherry-Pick

bash
# Switch to the target branch
git checkout main

# Cherry-pick a specific commit
git cherry-pick abc1234

This command takes the commit with hash abc1234 and applies its changes to the current main branch, creating a new commit.

Cherry-Picking Multiple Commits

bash
# Apply multiple commits in sequence
git cherry-pick abc1234 def5678 ghi9012

Cherry-Picking from Another Branch

bash
# First checkout the branch containing the commit
git checkout feature-branch

# Then cherry-pick to your target branch
git checkout main
git cherry-pick feature-branch~1

As GitLab Docs states, this approach lets you “copy specific changes from an existing branch into your current branch.”

Conflict Resolution

When cherry-picking introduces conflicts, Git stops the process and leaves you in a conflicted state. Here’s how to handle them:

  1. Resolve conflicts: Edit the conflicted files manually
  2. Stage resolved files: git add <resolved-file>
  3. Continue cherry-pick: git cherry-pick --continue
  4. Abort if needed: git cherry-pick --abort

The Medium article on mastering git cherry-pick provides excellent guidance on conflict resolution strategies and best practices.

Advanced Options

--no-commit

Apply the changes but don’t create a new commit:

bash
git cherry-pick --no-commit abc1234

-x

Add original commit author and message to the new commit:

bash
git cherry-pick -x abc1234

--mainline

For merge commits, specify which parent should be considered the mainline:

bash
git cherry-pick --mainline 1 abc1234

--edit

Edit the commit message before creating the new commit:

bash
git cherry-pick --edit abc1234

Comparison with Other Git Commands

vs Merge

  • Merge: Combines entire branch histories, creates merge commits
  • Cherry-pick: Selective commit application, preserves linear history

vs Rebase

  • Rebase: Moves entire branch to new base, rewrites history
  • Cherry-pick: Applies specific commits, creates new commits

vs Reset

  • Reset: Moves branch pointer back, potentially discards changes
  • Cherry-pick: Creates new commits, preserves original history

As noted in the GeeksforGeeks tutorial, cherry-pick is like “copying a single change without merging the entire branch,” making it fundamentally different from these other operations.

Conclusion

Git cherry-pick is an essential command for selective commit integration across branches. It allows developers to:

  1. Precisely control which commits get integrated into different branches
  2. Maintain clean histories by avoiding unnecessary merges
  3. Quickly apply fixes across multiple branches
  4. Handle complex workflows with granular control over changes

The command works by taking changes from a target commit and applying them to your current branch, creating a new commit in the process. While powerful, it should be used thoughtfully to avoid creating duplicate commits or confusing branch histories.

For best results, combine cherry-pick with good commit hygiene, clear commit messages, and proper conflict resolution techniques. When used appropriately, it becomes an indispensable tool in the Git workflow toolkit.

Sources

  1. Git Cherry Pick - How to use the “cherry-pick” command in Git | Learn Version Control with Git
  2. Git Cherry Pick | Atlassian Git Tutorial
  3. Git - git-cherry-pick Documentation
  4. What does cherry-picking a commit with Git mean? - Stack Overflow
  5. Git - Cherry Pick - GeeksforGeeks
  6. Git Cherry Pick - How to Cherry Pick a Commit in Git | Learn Git
  7. Git Cherry-Pick: How to Select and Apply Specific Commits | DataCamp
  8. Mastering Git Cherry-Pick: Advanced Guide with Real-World Examples | Medium
  9. How to cherry-pick commits in Git. A Step-by-Step Guide | Medium
  10. Cherry-pick changes with Git | GitLab Docs