How to Cherry-Pick a Commit from Another Branch in Git

Imagine you’re working on a software project using Git, and there’s a brilliant feature or a bug fix in another branch that you desperately need in your current branch. Re-merging everything might not make sense, especially if you’re not ready to bring in all the changes. This is where the concept of cherry-picking comes in—a handy tool in Git that allows you to apply the exact commit you need, wherever you need it.

TLDR (Too long, didn’t read):

Cherry-picking in Git lets you take a specific commit from one branch and apply it to another, without merging all the code. It’s helpful when you want to isolate and transplant one change without bringing along all the accompanying commits. Use it wisely to avoid unnecessary complexity or potential code conflicts. Whether you’re fixing bugs or cherry-picking features, understanding this Git command can make you a more efficient developer.

What Is Git Cherry-Picking?

In simple terms, cherry-picking means selecting one commit from a branch and applying it to another branch. Think of it as picking the cherries (commits) you want and leaving the rest. Cherry-picking is different from merging or rebasing, which involve more comprehensive changes. It’s great for small, specific updates such as applying a critical bug fix from the production branch to the development branch.

When Should You Use Cherry-Picking?

Cherry-picking is incredibly useful in several situations. Here are some scenarios where it makes sense:

  • Critical Patches: You need to apply a hotfix from your production branch to your feature branch without merging everything else.
  • Selective Features: A teammate finished a feature in another branch, and you’d like to use it in yours without taking on their entire branch changes.
  • Code Reusability: You want to reuse logic or functionality across different modules handled in separate branches.

Preconditions Before Cherry-Picking

Before you cherry-pick a commit, ensure:

  • You have access to the commit hash of the commit you want to cherry-pick.
  • You’re on the target branch where you want to apply the commit.
  • You’ve pulled the latest changes from all relevant branches.

Being cautious is key—conflicts can occur just like with merging or rebasing, so it’s better to work on a clean, updated workspace.

How to Cherry-Pick a Commit

Now, let’s go through the process step by step:

  1. Checkout the target branch:
  2. git checkout main

    This moves you to the branch where you want to apply the commit.

  3. Locate the commit hash you want to cherry-pick:
  4. git log branch-name

    This opens a list of commits so you can find the specific one. Copy its hash (usually a long alphanumeric string).

  5. Cherry-pick the commit:
  6. git cherry-pick <commit-hash>

    This applies the changes from that commit into your current branch. If it’s successful, the commit is added directly, preserving the original message.

Cherry-Picking Multiple Commits

You might want to move multiple commits at once. You can do this using ranges:

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

This command cherry-picks a range of consecutive commits. The caret (^) before the start commit includes it in the range.

If the commits are not consecutive, you can cherry-pick them individually using:

git cherry-pick <commit1> <commit2> <commit3>

Handling Conflicts When Cherry-Picking

Git may throw conflicts during cherry-picking, just like during merging. If this happens:

  • Git will pause the process and mark conflicted files.
  • Edit the files to manually resolve conflicts.
  • Once resolved, use:
git add .

Then continue with:

git cherry-pick --continue

If things go sideways and you need to cancel, use:

git cherry-pick --abort

This will stop the operation and return your branch to its pre-cherry-pick state.

Tips for Effective Cherry-Picking

  • Use with caution: Overusing cherry-pick can lead to disorganized commit histories or duplications.
  • Communicate with your team: Make sure everyone understands why specific commits are being copied across branches.
  • Keep notes: Document cherry-picked commits in pull requests or commit messages for future clarity.
  • Don’t cherry-pick merge commits unless necessary: They can introduce complications and are better handled with other methods.

Undoing a Cherry-Picked Commit

If you realize that you mistakenly cherry-picked a commit, you can roll back using:

git revert <commit-hash>

This will create a new commit that undoes the changes introduced by the cherry-picked commit, while keeping your history intact.

GUIs and Cherry-Picking

If you’re not comfortable using the command line, many graphical Git tools support cherry-picking. Here are a few worth mentioning:

  • GitKraken: Select the commit and drag it to your current branch to cherry-pick.
  • Sourcetree: Right-click on the commit and choose “Cherry Pick.”
  • GitHub Desktop: While it doesn’t support cherry-picking out of the box, other tools in the GitHub ecosystem can help.

Using GUI tools can make complex operations like cherry-picking more intuitive, especially for beginners.

The Benefits of Knowing Cherry-Picking

Mastering cherry-picking grants you the surgical precision to make independent commits travel across branches. It’s empowering for fast-paced development environments, bug squashing, and code sharing across teams. While it’s not the answer for every situation, it offers remarkable flexibility when used with intention.

Conclusion

Cherry-picking is one of Git’s many powerful features, allowing developers to grab just the right commit at just the right time. Whether you’re applying critical fixes or moving partial features across environments, knowing how to cherry-pick makes you more agile and precise. Like all Git commands, it requires a good understanding of your codebase and coordination with your team. Used wisely, it can save hours of unnecessary merging and swapping branches.

So, the next time you need that one commit—and only that one—remember: cherry-picking can help you have your cake and eat it too.