What is git cherry-pick and how to use it?

Git is a version control system used to version control the source code. It helps programmers to work on same feature and commit their code simultaneously without interrupting each other. Many modern code editors like Sublime, bracket and Atoms comes with Git support.

You can install these editors and start committing your code directly to git without installing any other plugin or software. Now lets move to git cherry-pick.

What is git cherry-pick

The git cherry pick is a powerful command from Git that allows the user to select specificcommitsto bring to the desired branch.

When working in teams on a project, there are times when more than one developer works on the same code.

For example, when the back-end developer creates a data structure that the front-end will also use. The front-end developer can use git cherry pick to continue working on the commit that has the database, but in his own way.

What is the purpose of the git cherry pick command?

Imagine the following case, you are working on the administrator registration branch, and your colleague is working on another branch related to user registration.

In this work, your partner ends up encountering a problem that affects both of you, and makes the correction, but his task is still in progress.

Therefore, you cannot merge, but you need correction.

But isn’t it just giving a merge?

We may have some problems when we merge a code that has not yet been finalized, or revised, if you use PR.

You can try to rebase or merge the branch that your colleague solved an error, but conflicts can happen.

In other words, you will be bringing unnecessary code to your branch, which may have errors or have not yet been tested.

How git cherry pick works

All commits have an ID, something like “ f23bc3c…“. It is this identifier that you need to take from your coworker’s branch in order to copy it to yours.

The steps are very simple, first go to your colleague’s branch

git checkout "branch name"

Find his commit ID, you can do that with git log.

Just to illustrate, I will use this ID to make the explanation easier: “d13c13c3541d26f805d6047495867e39387f54203”

Go back to your branch, and type

git cherry-pick d13c13c3541d26f805d6047495867e39387f54203

Ready! With that, you copied only the commit you needed, without wasting time. That is why it is important to always work on making small commits.

Imagine if your colleague had just created a commit with the content of the entire workday, we would have a big problem.

Cherry pick came to help us with this problem! In case the task is still in progress, he can get a commit and copy it from branch to branch.

Getting a commit interval

In the previous example, I showed how you copy just one commit from one branch to another, but you can work with a range of commits.

In that sense, think that your colleague, like me, is very fond of creating small commits. Now for one feature to add you have to merge multiple commits to your branch.

To solve the problem, cherry pick allows you to enter the ID of the initial commit and the ID of the final commit, which here I will callAandB, respectively.

So, when we talk about range, you have two options:

Copy all commits, including the first

git cherry-pick A^..B

Or ignore the first commit:

git cherry-pick A..B

It is important to note that commitAmust be after commitB , that is, be older in the timeline.

Conflicts in the git cherry pick

As with merges and rebases, cherry picks can also cause conflicts, and you will resolve them in the same way as a regular merge or rebase.

You can solve them using a graphical interface, or via terminal.

From the terminal, we have two commands: thecontinue,which is for you to execute after solving the conflict in the code.

git cherry-pick --continue

And we have the abort, to cancel the cherry pick.

git cherry-pick --abort


In summary, these are the most common commands that can help you:

  • -e : allows you to edit the commit message.
  • -x: add a message to the copied commit saying it is a cherry pick from another commit – “cherry picked from commit”.
  • –Allow-empty : by default, cherry-pick does not allow blank commits, with this parameter, it overrides this behavior.
  • –Allow-empty-message : when the commit does not have a title, it is barred. As in the previous example, this parameter overrides the behavior.

Watch out

git cherry-pick is a very useful command when used well, but it cannot replace git merge and git rebase. Each has a situation that fits them better, but that is the subject for a different article.

Use the cherry-pick only as a last resort, be careful not to duplicate commits in your timeline, use it sparingly!

What did you think of the content? Did you already know the git cherry pick command? Have you used it in your project? Leave a comment and let me know if I missed anything!

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *