There are scenarios when we want to apply the changes of an earlier commit in a repo, on top of the current repo tree of some branch in the same repo.
Or
In some scenarios, we just want all the changes from an earlier commit of this repo into the current staging area and make some more changes before finally committing all the changes to the repo tree.
For both scenarios, the git cherry-pick command comes in handy.
Git documentation describes cherry-pick as
git-cherry-pick — Apply the changes introduced by some existing commits.
Given one or more existing commits, apply the change each one introduces, recording a new commit for each
Let’s explain this with an example
Step 1- Create a git repo
mkdir git-example-cherry-pick && \
cd git-example-cherry-pick && \
git init
Step 2- Add a few commits
echo "First change" >> first.file && \
git add first.file && \
git commit -m "first commit"
echo "Second change" >> second.file && \
git add second.file && \
git commit -m "second commit"
echo "Third change" >> third.file && \
git add third.file && \
git commit -m "third commit"
If you’ve been following my earlier posts in the git series, you already know that commits are the first class citizen in Git. Branches have very small significance as they are merely named pointers for the HEAD of a repo tree.
In this example, we will see, that it doesn’t matter if cherry-pick is done across branches or within the same branch.
Step 3- Switch to a new branch and add a commit
git checkout -b branch-one
echo "4 change" >> 4.file && \
git add 4.file && \
git commit -m "4 commit"
Step 4- Switch to another branch and add a commit
Let’s create a new branch named branch-two. We will create this branch out of the master branch, so the file named 4.file created in branch-one in step 3 will not be present in this new branch branch-two.
git checkout -b branch-two master
echo "5 change" >> 5.file && \
git add 5.file && \
git commit -m "5 commit"
Step 5- Use cherry-pick
Now if you see the files in the current folder, you will not see a file named 4.file. This file was created as part of a 4th commit which was applied only on branch-one
To apply the same changes in the current branch, we need the commit hash of the commit. git log command can be used for the same.
This is what I see when I look at the git logs for branch-one.
Now we are on branch-two, Let's apply the change from commit by using the cherry-pick.
git cherry-pick 0938880
This takes all the changes done under the mentioned commit & applies them on top of the current HEAD. In our case, one new file named 4.file will be added.
Duplicate commits problem
With the cherry-pick, the exact same changes are applied with a new commit SHA identifier. In other words, cherry-pick causes duplicate commits.
If you really needed the code from an earlier commit which you are cherry-picking as a base from your own further changes, you can use the command with the — no-commit option.
This will not create a duplicate commit but will put the changes from that earlier commit into your working directory.
Now you can make your changes on top of this code and then commit.
git cherry-pick 0938880 --no-commit
This is all for a simple but very useful cherry-pick command.
Comments