Git

How to Cherry Pick a Commit From One Branch to Another?

As a distributed version control system, Git permits the user to track the changes within a branch via commits. Additionally, Git enables the user to work in branches. Developers can create other branches of projects, work on them, and at the end merge. However, there are various scenarios in which users don’t want to merge all the branches but only one or a few commits. To handle such cases, Git has its “cherry-pick” command that can only pick the specific commit from the particular branch and apply it to the current branch.

In this guide, we will learn the steps to cherry-pick a commit from one Git branch to another.

How to Cherry Pick a Commit From One Git Branch to Another?

To cherry-pick a commit from one branch to another, let’s work in one branch, create a new branch, and cherry-pick the commit from our previous branch. To do so, accomplish the below-stated steps.

Step 1: Move to Project Repository

Open Git Bash and move to the project directory by executing the “cd” command:

cd cherry-pick

Step 2: Initialize Repository

Afterward, initialize the Git repository with the provided command:

git init

Step 3: Create New File

Create a file and add the content to it using the “echo” and redirection operators (>). For instance, the “file.txt” file will be created:

echo "New File" > file.txt

Step 4: Track File

Add the created file to the tracking area through the “git add” command:

git add .

Step 5: Commit Changes

Commit the changes in the project directory via the “git commit” command:

git commit -m "file added"

Step 6: Check Log

Check the long history of the applied commits by running the log command:

git log --oneline

Here, you can see the SHA for the particular commit. Note down it, we will use it and cherry-pick it in the later steps.

Step 7: Create New Branch

Now, let’s create a new branch named “develop” with the given command:

git branch develop

Step 8: Switch to Created Branch

Switch to the created branch through the “git checkout” command:

git checkout develop

The branch has been switched to “develop”.

Step 9: Cherry Pick a Commit

To cherry-pick the commit, run the “git cherry-pick” command along with the SHA hash of the previous branch commit:

git cherry-pick 6ea44fe

The above output shows the conflict, just ignore it and run the suggested command to do it anyway.

git commit --allow-empty

The commit has been cherry-picked.

Step 10: Verify Log History

Verify the log history to check that the commit has been cherry-picked:

git log

As you can see in the above image our commit from the previous branch “master” has been cherry-picked.

Conclusion

To cherry-pick a commit, copy the SHA hash of the commit and move to the second branch. Then, run the “git cherry-pick <SHA Hash>” command along with the commit SHA hash. Verify the log history that the commit has been cherry-picked.

About the author

Abdul Mateen

I hold a bachelor's degree in Computer Science and have a good technical background. As an author, I break down technical concepts in a clear and concise manner. Learning new skills and exploring new technologies is my passion.