Cherry-Pick

Without question, cherry-pick is a powerful tool in git. It enables you to pick a commit and add that on top of the current HEAD. In some cases, it may just save you from a terrible mistake while resolving some conflicts or unwanted commit merges. For switching branches and to bring commit/commits to the new branch cherry-pick is the best and easiest option you have.

Now, let’s say you have switched to a new branch from an old branch that you were working on. And now you want to cherry-pick some commits from the old one to the new one. What do you usually do?

I did the following ⬇️ until I came to learn about the special feature.

Syntax

    git cherry-pick CommitHash

Example

Let’s say you have the following commits ⬇️

    c9
    c8
    c7
    c6
    c5
    c4
    c3
    c2
    c1

And you want to pick c3 to c8. I can only imagine the face of someone picking them one by one 🤣. Anyways, from git 1.7.2, you can pick a range of commits.

To pick commits from c3 to c8 you have to write down the two commit hashes with two dots in between. It works with the first 6 digits of the hash too.

Comand One

    git cherry-pick c3..c8

This will pick commits after c3 to c8. Will not include c3.

If you want to include c3 as well, you have to add this 👉 ^ special syntax.

Command Two

    git cherry-pick c3^..c8

NOTE: Watch out for the commits that have conflicts.