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.
Prerequisites
- Git installed (follow our tutorials to install Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
- A separate branch with multiple commits.
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
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
:
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
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
:
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.