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.