Better Programming

Advice for programmers.

Follow publication

Git Cherry-Pick — Selecting Specific Commits to Merge

Carlos Fernando Arboleda Garcés
Better Programming
Published in
4 min readJan 4, 2021
Git branches
Photo by Yancy Min on Unsplash.

As you know, when you are working with Git and need to do a merge between two branches, every commit in the source branch will be merged in the target branch without exception. Well, I’m going to tell you a scary story about this very topic and how I managed to solve it.

Recently, I joined a new software company called Patagonian. A few days after I started there, the project manager assigned a task to me that belongs to a bigger task (an epic task).

When I finished working on my task, I created a pull request, did a merge to the develop branch, and continued with my next task. A few days later, we needed to make a release to staging and then to production, but… an incomplete epic task was merged to develop and would be part of the next release.

Therefore, we could not do a normal release by merging develop to staging. Actually, we needed to exclude the epic task’s commits from the release. Can you imagine my worry?

Fortunately, my teammates had already solved this before and had an ace up their sleeve: git cherry-pick. This Git command makes it possible to apply existing commits to another branch by picking the specific commits that you need.

cherry-pick

As you can read in the official documentation, the cherry-pick command allows you to choose the specific existing commits to include in another branch.

To make this easier to understand, I will use a graphical example. Imagine you have a repository with some commits in your develop branch, but some of the changes are part of an incomplete feature that was merged by error (green cherries). In that case, you need to pass only the commits related to finished features (red cherries) to the main branch.

Diagram showing how cherry-pick works
Illustration of cherry-pick command.

Here are some screenshots that show how to create a cherry-pick using Sourcetree. Continue reading the terminal explanation if you want to understand how it works.

Selecting commits to be included in cherry-pick command
Selecting commits to be included in cherry-pick command
Setting up and creating cherry-pick using Sourcetree
Set up and create cherry-pick using Sourcetree

How Can I Use This With Terminal?

If you are terminal lover, maybe you will want to do this process using your pretty terminal. Well, consider the following steps.

Firstly, you have to create a new branch named main_cherry_pick from your target branch main. This branch will be used to apply the selected commits and then merge them into the main branch.

Secondly, you need to know the commit IDs that you want to include. In the develop branch, execute the next log command that will show the hash and massage for each commit:

git log --pretty=format:"%h %s" 

You should see something like this:

Result of Git log command
Result of Git log command

The next step is to check out the target branch. Then copy the needed IDs and execute the cherry-pick command. In this case, I will copy the commit IDs related to files 1, 2, 3, 5, and 6, omitting files 4, 4.1, and 4.2 since those belong to an incomplete feature.

git checkout main_cherry_pick
git cherry-pick 12670fd 14b185a df931e3 1441727 2296bb0

If you don’t have any conflict, you will see one commit confirmation for each selected/included commit in your cherry-pick command:

Output

Finally, you need to push the changes in your main_cherry_pick to the remote branch. Then you have to do a normal merge from main_cherry_pick into the main branch.

Conclusion

What I have explained here is the most basic use of git cherry-pick. There are several options that you can check out in the official documentation:

Thanks for reading! I hope that this article has been helpful. All constructive feedback is welcome.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Carlos Fernando Arboleda Garcés
Carlos Fernando Arboleda Garcés

Written by Carlos Fernando Arboleda Garcés

Senior Software Engineer at EPAM, Co-founder at Mobile Lab, Co-founder at La Manicurista, Former CTO at La Manicurista, Organizer at GDG Cali, GenAI enthusiast

Write a response