All articles
Article 3 min read

Mastering Cherry-Pick in GitHub: A Practical Guide

Cherry-picking in Git allows you to select specific commits from one branch and apply them to another. This guide walks you through the steps of this useful feature.

Introduction

Cherry-picking is a powerful feature in Git that enables developers to apply specific commits from one branch to another without having to merge entire branches. This capability is particularly useful when you need to apply a bug fix from a development branch to your main branch or when you want to copy specific changes without merging all changes in a branch.

In this article, we will explore the process of cherry-picking in Git and GitHub, covering the prerequisites, steps, and potential pitfalls to be aware of.

Prerequisites

Before you begin cherry-picking, make sure you have the following:

1.

Git Installed: Ensure that Git is installed on your machine and that you have access to the repository where you want to perform the cherry-pick operation.

2.

GitHub Access: You should have the necessary permissions to modify branches in the repository you are working with.

3.

Branch Awareness: Familiarize yourself with the branches in your repository, as you will need to identify the specific commit you want to cherry-pick.

Finding the Commit Hash

To cherry-pick a commit, you first need to identify its unique hash. This can be done using the following command:

bash
git log --oneline

This command will list all commits in the current branch in a concise format. Look for the commit you want to cherry-pick and note its hash (the alphanumeric string at the beginning of the line).

Cherry-Picking a Commit

Once you have identified the commit hash, follow these steps to cherry-pick the desired commit:

1.

Checkout the Target Branch: Switch to the branch where you want to apply the commit.

bash
   git checkout target-branch

Replace target-branch with the name of your target branch, e.g., main or develop.

2.

Execute Cherry-Pick: Run the cherry-pick command with the commit hash.

bash
   git cherry-pick <commit-hash>

Replace <commit-hash> with the hash you noted earlier.

3.

Resolve Conflicts If Necessary: If there are conflicts between the commit and your current branch, Git will notify you and pause the process. You will need to manually resolve these conflicts.

After resolving any conflicts, use:

bash
   git add <file-name>
   git cherry-pick --continue

If you want to abort the cherry-picking process, use:

bash
   git cherry-pick --abort

Example of Cherry-Picking

Here’s a complete example of cherry-picking a commit:

1.

Check out the target branch:

bash
   git checkout main
2.

Find the commit you want to cherry-pick:

bash
   git log --oneline

Suppose the output is:

text
   a1b2c3d Fix typo in README
   4e5f6g7 Add new feature
3.

Now cherry-pick the desired commit:

bash
   git cherry-pick a1b2c3d

If there are conflicts, resolve them as necessary, and then continue the cherry-pick with the appropriate commands.

Conclusion

Cherry-picking is a straightforward yet powerful technique in Git that allows you to selectively incorporate changes from one branch to another. By following the steps outlined in this guide, you can effectively manage commits in your repository, applying only those changes that are necessary for your work. Familiarize yourself with this feature to enhance your version control workflow and maintain a clean project history.