How to Cherry-Pick from Another Branch in Git

October 2, 2023

Introduction

Cherry-picking in Git is a useful operation that allows users to select and apply a single commit from one branch to another. It is an alternative to git rebase and git merge, which are used to apply multiple commits to another branch.

This tutorial will show how to cherry-pick a commit from another branch in Git.

How to cherry-pick from another branch in Git - a tutorial.

Prerequisites

What Is Cherry-Picking in Git?

Cherry-picking in Git is a process of choosing and applying specific commits from one branch to another. Through cherry-picking, users can extract one or several commits from one branch's history and apply them as new commits on a different branch.

The process helps incorporate specific changes, such as bug fixes or features, without merging the entire source branch. Picking individual commits facilitates management and integrates changes granularly within a Git repository.

When to Use Git Cherry-Picking?

Use cherry-picking in Git to select specific changes from one branch and apply them to another without merging the entire branch. It is a great option for selectively incorporating bug fixes, features, or other changes from one branch into another.

While cherry-picking is a valuable tool for selective changes, consider the context of your project and its potential implications on commit history, collaboration, and code dependencies. If there are possible negative implications of cherry-picking, opt for alternative Git strategies like merging or rebasing.

The situations when you need to avoid using Git cherry-pick are:

  • When changes depend on a series of commits. If the commit you want to pick depends on a series of commits in the source branch and cherry-picking them individually would break the intended functionality, merging or rebasing the entire branch is a better choice. Cherry-picking individual commits may result in the code not working as expected.
  • When it disrupts branch integrity. Cherry-picking can disrupt the chronological order of commits in a branch's history. Avoid cherry-picking if it is crucial to preserve the exact branch history.
  • When it duplicates commits. Cherry-picking can lead to duplicated commits if the same changes are cherry-picked into multiple branches. Doing so makes it hard to track changes and increases the risk of conflicts in the future.
  • When it causes merge conflicts. Cherry-picking can introduce complex merge conflicts, especially when the same changes have occurred in the source and target branches. In that case, it is simpler to merge the branches instead.

How to Use Git Cherry-Picking?

To cherry-pick in Git, utilize the git log command to pinpoint the commit you want to cherry-pick. Then, cherry-pick the commit onto the target branch using the commit ID.

Follow the steps below:

1. Find the commit ID by running:

git log
Checking the Git log for commit ID.

For this tutorial, we will cherry-pick the commit 71277daebbb157f7018a73e482e2379d9efceb64 (new file) from the master branch. Take note of the commit ID you want to cherry-pick, as you will need it later.

2. Switch to the branch you want to cherry-pick the commit to. The syntax is:

git switch [branch-name]

We will cherry-pick the commit to a branch named test-branch:

Switching branches in Git.

3. After switching to the target branch, cherry-pick the commit using the commit ID you copied from the log. The syntax for cherry-picking is:

git cherry-pick [commit id]

We will run the following command to cherry-pick the commit mentioned earlier:

git cherry-pick 71277daebbb157f7018a73e482e2379d9efceb64
Using cherry-pick on a commit in Git.

The command cherry-picks the specified commit from the master branch to the test-branch, adding a new commit in the branch history, as shown in the output of git reflog:

Checking the cherry-pick commit in git reflog.

Note: See how to undo the last commit using git revert.

Conclusion

Git cherry-pick is a powerful and versatile tool that allows to selectively apply specific commits from one branch to another. However, make sure to use cherry-picking with caution and ensure that the changes you apply are appropriate in the context of the project to avoid conflicts and code inconsistencies.

Next, see how to check out a file from another branch or learn to squash commits in Git.

Was this article helpful?
YesNo
Bosko Marijan
Having worked as an educator and content writer, combined with his lifelong passion for all things high-tech, Bosko strives to simplify intricate concepts and make them user-friendly. That has led him to technical writing at PhoenixNAP, where he continues his mission of spreading knowledge.
Next you should read
How to Overwrite Local Branch with Remote in Git
August 31, 2023

Overwriting a local Git branch with a remote one synchronizes the changes with the remote repo. This tutorial shows two different methods for overwriting a branch in Git.
Read more
How to Compare Two Git Branches
August 23, 2023

This tutorial shows several methods for comparing two Git branches. Comparing branches is important before merging to ensure all conflicts and bugs are eliminated before changing the main codebase.
Read more
How to Change Git Commit Message
July 11, 2023

Git is a version control system that allows users to track changes in their code. One of the available features is the ability to change a Git commit message, and this step-by-step guide shows how to do that.
Read more
Git Fetch: Definition & Examples
December 8, 2021

The git fetch command helps retrieve new contents from a remote repository, without applying the changes immediately. Follow this guide to learn how to use git fetch and obtain the latest changes stored in a remote repository..
Read more