Cherry-pick Specific Commits Across Branches

Summary: Apply individual commits using git cherry-pick.


When working with Git, there are many scenarios where you want to apply only specific changes from one branch to another, rather than merging or rebasing the entire branch. This is where git cherry-pick shines. In this article, we'll walk you through the process of cherry-picking commits, common pitfalls, and tips for effective use.

What is Cherry-picking?

git cherry-pick is a powerful command that allows you to take an individual commit (or a range of commits) from one branch and apply it onto another branch. Unlike merging or rebasing, which operates on entire branches, cherry-picking focuses on specific commits.

Use-case examples:

  • You fixed a bug on your feature/login branch but want to quickly apply that fix to main.
  • You accidentally committed a key update to the wrong branch and need to move it.

How Cherry-pick Works (Under the Hood)

Cherry-picking works by applying the change introduced in the selected commit(s) as a new commit on your current branch. The commit history will show the cherry-picked commit as a new commit, possibly with a reference to the original one in the message.

Note: The cherry-picked commit will get a new hash, since it is a new commit, not a copy.

Basic Syntax

git cherry-pick <commit-hash>

Parameters:

  • <commit-hash>: The hash of the commit you want to apply.

Step-by-step Guide

1. Find the Commit to Cherry-pick

First, identify the commit you wish to cherry-pick. You can view the commit history using:

git log --oneline

Output example:

a1b2c3d Fix login bug
4d5e6f7 Update README
8a9b0c1 Add user authentication

Suppose a1b2c3d contains the bug fix you need.

2. Checkout the Target Branch

Switch to the branch where you want to apply the commit:

git checkout main

3. Cherry-pick the Commit

Run:

git cherry-pick a1b2c3d

If the cherry-pick is successful, you'll see:

[main 1c2d3e4] Fix login bug
 Author: Your Name <you@example.com>
 Date:   Thu Jun 6 12:34:56 2024 -0400
 1 file changed, 2 insertions(+), 1 deletion(-)

4. Resolve Conflicts (if any)

If the commit cannot be cleanly applied (for example, the same lines were modified differently), Git will pause and ask you to resolve conflicts manually.

  • Edit the conflicting files to resolve the conflict.

  • Mark as resolved using:

    git add <filename>
    
  • Continue the cherry-pick:

    git cherry-pick --continue
    

If you want to abort the cherry-pick:

git cherry-pick --abort

5. (Optional) Cherry-pick Multiple Commits

You can cherry-pick a range of commits:

git cherry-pick <start-commit-hash>^..<end-commit-hash>

Example:

git cherry-pick a1b2c3d^..4d5e6f7

This will cherry-pick both a1b2c3d and 4d5e6f7 (inclusive).

Tips and Best Practices

  • Cherry-pick with Caution: Since cherry-picking creates new commits, be mindful of duplicate commits and potential merge conflicts.
  • Use clear commit messages: When resolving merge conflicts, you may be prompted to edit the commit message—add context if needed.
  • Avoid cherry-picking merge commits unless you understand the complexities. Use the -m option for parent selection, or preferably, cherry-pick individual changes.
  • Keep history clean: If cherry-picking a commit related to a bugfix, annotate the message to indicate the source branch for future reference.

Real-world Example

You're working on the dev branch and spot a useful bugfix in the feature/refactor branch.

# On dev branch
git log feature/refactor --oneline

# Suppose the commit hash is 1234abcd
git cherry-pick 1234abcd

Summary

git cherry-pick is a vital tool for bringing specific changes across branches without rebasing or merging whole branches. Use it to quickly and safely move those precise commits your project needs, reducing noise and making your workflow more flexible.

Further Reading:

Happy cherry-picking! 🍒