DEV Community

Philip Perry
Philip Perry

Posted on

Practical git cherry pick example

I do most of my learning when trying to solve an actual problem that I'm working on. But I thought it might be fun to randomly learn new things by reading the documentation and then trying it out. I plan to do this regularly now and write about my findings.

I had heard about git cherry-pick before, but never tried it out. After learning about it, I found I could have actually used it just the other day when doing a hotfix that needed to be in both the integration branch (for testing) and the main branch (on production).

I found the documentation a bit hard to follow in regards to git cherry-pick, so I asked ChatGPT to generate a practical example:

"Generate a practical example that I can try out on using git cherry pick"

Below is the answer from ChatGPT that I edited a bit, in order to make things a bit clearer (e.g. in regards to creating branches and using VS Code for merging).


Let's consider a scenario where you have a Git repository with two branches: feature and main. You've been working on the feature branch, and you've made some commits that you want to selectively apply to the main branch using git cherry-pick.

Here's a step-by-step example:

1) Create the Repository:
Create a new directory and initialize a Git repository:

mkdir git-cherry-pick-example
cd git-cherry-pick-example
git init
Enter fullscreen mode Exit fullscreen mode

Let's create a file on the main branch:

touch test.txt
git add test.txt
echo "First line of code" >> test.txt
git commit -m "first commit"
Enter fullscreen mode Exit fullscreen mode

2) Create Commits on the Feature Branch:

Create a feature branch and make a few commits on the feature branch:

git checkout -b feature

echo "Feature update 1" >> test.txt
git commit -am "Add feature update 1"

echo "Feature update 2" >> test.txt
git commit -am "Add feature update 2"
Enter fullscreen mode Exit fullscreen mode

3) Return to the main branch

git checkout main
Enter fullscreen mode Exit fullscreen mode

4) Cherry-Pick Commits:

Now let's say you want to apply only the first commit from the feature branch onto the main branch:

Note down the commit hash of "Add feature update 1"

git log feature --oneline 
Enter fullscreen mode Exit fullscreen mode

This will return something like this:

b245f30 (feature) Add feature update 2
93b448c Add feature update 1
d3bdfaf (HEAD -> main) first commit
Enter fullscreen mode Exit fullscreen mode
git cherry-pick <commit-hash> #(in my case I replace `<commit-hash>` with `93b448c`
Enter fullscreen mode Exit fullscreen mode

It now added the line "Add feature update 1" below the line "First line of code", so my file test.txt now looks like this:

First line of code
Feature update 1
Enter fullscreen mode Exit fullscreen mode

5) Review and Resolve Conflicts:

If I had added something to line 2 in test.txt before doing the cherry-pick, this would have resulted in a merge conflict. Let's simulate this by first reverting our cherry pick and then adding some random text to line 2 before doing the cherry pick:

git log --oneline #copy the commit hash of `(HEAD -> main) Add feature update 1`
git revert -m 1 <cherry-picked-commit-hash> #this will revert it so that we now just have "first line of code" again inside the file
echo "Second line of code" >> test.txt
git commit -am "Added second line of code"
git cherry-pick <commit-hash> #same commit hash we used further above for getting "Add feature update 1" from the feature branch
Enter fullscreen mode Exit fullscreen mode

This will now return the following message:

Auto-merging test.txt
CONFLICT (content): Merge conflict in test.txt
Enter fullscreen mode Exit fullscreen mode

If there are conflicts between the changes in the commit you're cherry-picking and the current state of the main branch, Git will pause the cherry-pick process and ask you to resolve the conflicts manually.
I find it easiest to resolve conflicts in VS Code, so I open the editor with code ., go to source control and click on "Accept both changes".

Image description

I save the file and then I either click on the "plus" symbol next to the file to stage it or in the terminal I can do git add test.txt

After resolving the conflicts, continue the cherry-pick process by running:

git cherry-pick --continue
Enter fullscreen mode Exit fullscreen mode

Top comments (0)