Git Cherry Pick

Dilani Alwis
3 min readMar 12, 2019
Related image

Some of you might already know about the cherry pick in Git, but for me, it was something I got to know recently. Therefore I thought its worth sharing.

Git cherry-pick is simply a git command which allows you to choose a specific commit from one branch and apply it to another branch.

This comes in handy when you want to merge only a few commits from your current branch to a different branch rather than merging the whole branch. This way it applies only the selected commits to the other branch.

Example scenarios -

  • You need to pick specific commits related to a feature and apply them to staging environment once the feature is tested in qa environment.
  • You need to release only the features your client has paid for.

OK… Now it is time to see how to do this actually.

Let’s say I have a project with two branches master and develop. Here all the development happens in develop branch and out from the commits I have in that branch, I will be moving the selected commits to the master branch.

Cherry pick a single commit

  1. Checkout to the branch where you have the commit which needs to be moved. In this case, it would be the develop branch.
Checkout develop branch

2. From develop branch run git — — oneline to get a better log of the develop branch history.

Log git history

3. Now pick the commit you want to move to the other branch. In this case I’ll be moving Add login feature which has the git hash a68f7f0.

4. Now checkout the branch where you want to apply this commit. In this case, the master branch.

5. Run command git cherry-pick <git-hash> by replacing the <git-hash> with the selected commit hash.

git cherry-pick the selected commit

This will cherry pick the commit with hash a68f7f0 and add it as a new commit on the master branch. If you run a git log in master branch you could see it is having a new (and different) commit ID.

master branch git log

Cherry pick more than one commit

Mention the commit ids separated by space in the cherry-pick command.

git cherry-pick more than one commit

This will cherry pick the commits with hash 5025426 and 0e6e6cf.

In both cases, if the cherry-picking gets halted due to conflicts then:

  • resolve the conflicts
  • run git cherry-pick — — continue

I hope this will be useful for you at some point of your day to day coding.

Happy cherry picking :)

--

--