How to Cherry-pick a commit in GitHub

As its name suggests, Cherry-picking on GitHub is a valuable developer feature. It allows them to apply selective commits from one existing branch to another. The Cherry-pick technique is very beneficial when developers want to use a bug fix; some branches need a feature from another without getting merged, recover lost commits, or selectively apply changes from a pull request. This article will familiarize us with the commands used to work with Cherry-pick in Git and GitHub.

Steps to Cherry-Pick in Git

Clone the Repository (if you haven’t already):

git clone https://github.com/your-username/your-repo.git
cd your-repo

Checkout the Target Branch:

Check out the branch where you want to apply the changes. Replace the “target-branch” with the name of your desired branch (e.g., main or develop).

git checkout target-branch

Make sure the branch is up-to-date:

git pull origin target-branch

Find the Commit to Cherry-Pick:

Identify the commit hash (SHA) you want to cherry-pick. You can find this hash by viewing the commit history.

git log

Cherry-Pick the Commit:

Use the git cherry-pick command followed by the commit hash.

git cherry-pick

For example:

git cherry-pick abcdef1234567890abcdef1234567890abcdef12

Resolve Conflicts (if any):

If there are any conflicts, Git will pause the cherry-pick and allow you to resolve them. After resolving the conflict, add the resolved files to the staging area.

git add

Continue the Cherry-Pick:

After resolving conflicts and staging the changes, continue the Cherry-pick process.

git cherry-pick --continue

If you want to abort the cherry-pick at any point:

git cherry-pick --abort

Commit and Push the Changes:

Once the cherry-pick is successful, commit the changes and push them to the remote repository.

git push origin target-branch

Other Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.