This content is part of the Essential Guide: Master Git basics and branch into DVCS

Essential Guide

Browse Sections
News Stay informed about the latest enterprise technology news and product updates.

How to 'git cherry-pick' from another branch example

In a previous tutorial, we took a look at how to cherry-pick a commit on the current branch, but one of the ancillary questions that commonly arises is how to perform a git cherry-pick from another branch. The topic of how to git cherry-pick from another branch, along with what the result of such an operation would be, is the focus of this tutorial.

As with all git tutorials, this one will start off with a clean repository and an empty working directory, which means the first step is to create a new folder, which I will name git cherry-pic example. The next step is to issue a git init call from within that folder.

/c/ git cherry-pick example (master)
$ git init
Initialized empty Git repository in C:/_git-cherry-pick-example/.git/

Preparing a branch for a git cherry-pick

With the repository initialized, the next step is to create three new files, adding a commit after each individual file is created. Since the repo was just initialized, all of this will occur on the master branch.

/c/ git cherry-pick example (master)
$ echo 'abba' > abba.html
$ git add . | git commit -m '1st commit: 1 file'
$ echo 'bowie' > bowie.html
$ git add . | git commit -m '2nd commit: 2 files'
$ echo 'chilliwack' > chilliwack.html
$ git add . | git commit -m '3rd commit: 3 files'

We are about to git cherry-pick from another branch, and specifically, we will be pulling in the second commit, but before we do we will delete all of these files and perform a commit to put the master branch back into an empty state.

/c/ git cherry-pick example (master)
$ rm *.html
$ git add . | git commit -m ‘4th commit: 0 files’

[master d6a8ce2] 4th commit: 0 files
3 files changed, 3 deletions(-)
delete mode 100644 abba.html
delete mode 100644 bowie.html

Inspecting the commit history

Issuing a git reflog command will show the rich commit history of the master branch. Note the hexadecimal id of the second commit, 63162ea, as this is the one we will use when we git cherry-pick from another branch.

/c/ git cherry-pick example (master)
$ git reflog
d6a8ce2 (HEAD -> master) HEAD@{0}: commit: 4th commit: 0 files
bc0f7d1 HEAD@{1}: commit: 3rd commit: 3 files
63162ea HEAD@{2}: commit: 2nd commit: 2 files
6adc6ff HEAD@{3}: commit (initial): 1st commit: 1 file

Switching to a feature branch

We will now create and move development onto a new branch named feature.

/c/ git cherry-pick example (master)
$ git branch feature
$ git checkout feature
Switched to branch 'feature'
/c/ git cherry-pick example (feature)

We will then create one file named zip.html and commit this file in order to create a small history of development on the feature branch.

/c/ git cherry-pick example (feature)
$ echo 'zip' > zip.html
$ git add . | git commit -m '1st feature branch commit: 1 file'

The next step is to git cherry pick from another branch to this new one, but before we do, think about what the expected result is. We will cherry-pick the 2nd commit from the master branch, namely the commit where the file named bowie.html was created. In the other branch, the bowie.html file sits alongside the abba.html file, which was created prior. What will the cherry-pick bring back? Will it bring back the abba.html and bowie.html files? Will it resurrect just the bowie.html file? Or will the command fail as we try to git cherry-pick across branches? Let’s see what happens.

How to git cherry-pick across branches

The id of the bowie.html commit was 63162ea, so the command to git cherry-pick is:

/c/ git cherry-pick example (feature)
$ git cherry-pick 63162ea
[feature d1c9693] 2nd commit: 2 files
Date: Thu May 17 17:02:12 2018 -0400
1 file changed, 1 insertion(+)
create mode 100644 bowie.html
$ ls
bowie.html zip.html

The output of the command to git cherry-pick from another branch is a single file being added to the current working tree, namely the bowie.html file. The directory listing command issued above shows two files, the zip.html file and the bowie.html file, indicating that the only change to the working tree was the addition of the second file.

How git cherry-pick works

As you can see from this example, when you cherry-pick, what is returned is not the entire state of the branch at the time the commit happened, but instead, only the delta between the commit that happened and the state of the git repository prior to the cherry-picked commit.

It should also be noted that any time you git cherry-pick from another branch, a new commit gets registered in the branch history, as is evidenced by the following reflog:

Needing to git cherry-pick from another branch is a common occurrence during software development cycles. As you can see from this example, so long as the hexadecimal id of the commit is known, performing a git cherry-pick from another branch is a safe and rather simple function to perform, especially if the branch doing the cherry-pick can merge the change without any clashes or conflicts occurring.

Become a Git power user

Want to become a Git power user? Take a look at the following Git articles and tutorials



App Architecture
Software Quality
Cloud Computing