git cherry pick commit examples explained [PROPERLY]

 

Introduction to git cherry pick

Did you know that you can choose a specific commit and apply it to another branch without tempering with the history?
Yes, this is possible using the git cherry-pick command in the git environment. For instance, if you mistakenly apply a commit to a branch that you didn’t intend to apply those changes, git cherry-pick will help you select that specific commit back to the intended branch.

The command git cherry-pick commit applies the changes introduced by the named commit on the current branch. It will introduce a new, distinct commit. Strictly speaking, using git cherry-pick doesn’t alter the existing history within a repository; instead, it adds to the history.

Advertisement

In this tutorial, we will look at the steps to apply cherry-picking in git. Also, we are going to cover the different methods of using cherry-pick in git with examples.

 

git cherry pick syntax

Following is the syntax of of git cherry-pick with all the list of supported options:

git cherry-pick [--edit] [-n] [-m parent-number] [-s] [-x] [--ff]  [-S[]] …​
git cherry-pick (--continue | --skip | --abort | --quit)

 

git cherry-pick workflow

Following is the general workflow of performing git cherry-pick:

## Switch to the branch that you would like to merge with
git switch master

## Identify the commits that you would like to carry to your active branch
git log --oneline

## Proceed to pick the relevant commit hashes you require for your selected branch
git cherry-pick -x <commit-hash>

## If you have notes attached to the commit they do not follow the cherry-pick. 
## To bring them over as well, You have to use:
git notes copy <from> <to>

Let us understand this with simple example:

Here we have two branch i.e. main and feature branch with different commit ids:
git cherry pick commit examples explained [PROPERLY]

During the course of normal development, a bug is fixed on the main line with commit F. Now we plan to add that bug fix from commit F also in the feature branch so we will use the following command:

Advertisement
$ git checkout feature
$ git cherry-pick F    ## <F is the commit id>

Following is the diagram explaining the behaviour:
git cherry pick commit examples explained [PROPERLY]

 

Set up the lab environment

Before we further dive into the topic, first ensure that you have your local workstation already set up.

I’m going to run the git cherry-pick experiments using windows 10 pro on my local workstation. I will clone my remote repojira _test by applying the clone syntax:

$ git clone https://github.com/git-test/jira_test.git

Sample output:
git cherry pick commit examples explained [PROPERLY]

Why do we use cherry-pick in git

You can use the git cherry-pick tool for several reasons:

  • To create changes in sub-branch without affecting the history of the main branch
  • It allows the combining of different branch versions in a situation that doesn’t allow merging an entire branch.
  • It is vital for bug fixing during testing as it applies to a specific branch and its commits
  • It provides an alternative way of handling mistakes to other commands like the git diff
  • Helps to restore misplaced/stale commits and undo commits with mistakes

 

Different examples of using git cherry-pick

In this section we will cover different scenarios where you can use git cherry-pick command with multiple examples for clear understanding:

 

Example -1: Pick commit hash from one branch to another

You will first use git log --oneline command to highlight the commits that you need from the other branch of interest like this:

$ git log --oneline <branch>

For this practice, we will use main as the branch with the commits we want to cherry-pick to our feature branch. We start by exploring the commits found in the main branch using git log --online as shown below:

Advertisement
$ git log --oneline main

Sample output:
git cherry pick commit examples explained [PROPERLY]

Now that we have commits from the main branch displayed out as shown by their commit codes and statements above. Next, we will cherry-pick the sample commit-six- 84743de from the displayed commits in the main branch to our feature branch like this:

First step is to switch to the branch where you plan to add your commit hash:

$ git checkout feature
Switched to branch 'feature' 
Your branch is up to date with 'origin/feature'.

Next perform the cherry pick action:

$ git cherry-pick 84743de
[feature d8d8d68] added fetfile.txt git commit -m added
Date: Fri Aug 6 17:03:03 2021 +0300
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 featfile.txt

It is a success! You now have a new commit hash named fetfile.txt in your feature branch placed at the top.

 

Example-2: git cherry-pick commit using original reference

To maintain the commit original reference for easy tracking of all the commits done on your active branch, you can still use git cherry-pick. To successfully achieve this, you will have to add x to your cherry-pick command like this:

$ git cherry-pick -x <commit>

The default git cherry-pick command won’t reference your original commit other than just identifying it as a new commit hash. Let’s use an example to understand the distinction better.

We will cherry-pick commit-two-2ebef67 from our main branch to the feature branch using git cherry-pick -x <commit> as illustrated below:

First checkout the feature branch:

$ git checkout feature
Switched to branch 'feature'
Your branch is ahead of 'origin/feature' by 1 commit.
  (use "git push" to publish your local commits)

Next perform the cherry pick action:

$ git cherry-pick -x cbeb8e8
[feature fc17f55] test3.txt
 Date: Fri Aug 6 19:24:15 2021 +0300
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 test3.txt
User@DESKTOP-PFQIL78 MINGW64 ~/Documents/GitHub/jira_test (feature)

Next, apply the git log command to now see the referencing details about the new commit.

$ git log
commit fc17f550e442317c551797a6ff0e7a8c2f6cd5e3 (HEAD -> feature)
Author: Josephine gittest@gmail.com
Date:   Fri Aug 6 19:24:15 2021 +0300
    test3.txt
    (cherry picked from commit cbeb8e856c45b8aab77d6f08dedafc878a31c180)

From this example, you can see that our commit hash has in-depth information on the original commit and the new commit message. It even shows its history that it was cherry picked while referencing the commit where it was picked from.

You can also see that head is pointing to feature as the active branch. Such details will help you understand better the history of the project branches and to avoid creating duplicates as well as creating cherry-pick conflicts.

 

Example-3: Pick multiple commits using git cherry-pick

It is possible to pick more than one commit once using git cherry-pick. Let us demonstrate this by using an example.

$ git cherry-pick <commit-A> <commit-B> <commit-C>

First, we will check out our feature branch; git checkout feature in our git_jira active working repo and then apply the above command:

Here we will practice picking two commits namely sample commit-seven and sample commit-8 using their codes from our main to feature branch using the example below.

$ git cherry-pick bc1f8fb 9e1f84b

Sample output:
git cherry pick commit examples explained [PROPERLY]

From the output above, you notice that we have been able to cherry-pick two commits using single command from the main branch to our feature branch. You can do the same if you want cherry-pick many more commits in the same sequence. If you would like to skip a commit then you will have to use $git cherry-pick --skip.

 

Example-4: Git cherry-pick for resolving conflicts

You may find yourself with cherry-pick conflicts while trying to cherry-pick a particular commit for your active branch. How do you fix that?

We will create an error by git cherry-pick sample commit-nine- cbeb8e8 which has already been cherry-picked from the main branch to the feature branch.

Sample output

$ git cherry-pick cbeb8e8
error: your local changes would be overwritten by cherry-pick.
hint: commit your changes or stash them to proceed.
fatal: cherry-pick failed

We have an error “your local changes would be overwritten by cherry-pick “raised. Git also goes to the extent of giving you a hint for resolving the error as indicated in the example. You can also see at the end that cherry-picking was unsuccessful.

To fix the error, apply the $ git cherry-pick --quit command which will take you back to the feature branch without any commits history.

$ git cherry-pick --quit

From here you can apply the git log command to see the available commits then correctly choose one. You may also opt to perform a new commit depending on the changes that you desire.

 

Useful git cherry-pick sequential commands

  • --abort: When applied to a conflict raised by git cherry-pick, it cancels the whole operation back to the primary state in the sequence.
  • --skip: When called out, it will skip the specified commit to the next in the sequence
  • --continue: Facilitates continuity of an operation after successful resolution of a cherry-pick conflict
  • --quit: Resets the git cherry-pick operation back to the active branch in the directory for a failed cherry-pick conflict.

 

Cherry pick is a great command to utilize in the git environment but you have to take a lot of precautions when using it. This is because if you aren’t careful, you will end up with lots of duplicate commits that will make it complicated to track your initial ones. That will further impact negatively on the project as it may cause delays.

An alternative to consider using in place of git cherry-pick is the git merge function.

 

Summary

We have been able to provide in-depth coverage on the following topics that will help you to effectively cherry-pick in git:

  • Git cherry-pick workflow
  • Git cherry-pick syntax
  • Steps to apply when using git cherry-pick
  • Methods of using cherry-pick
  • Useful git cherry-pick sequential commands

 

Further reading

git-cherry-pick-in-git

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment