Understanding Cherry Picking in Git | 2023

Amir Mustafa
6 min readSep 17, 2023

--

→ Git is a version control system for Project development.

→ Today, we will understand a simple, yet powerful concept of Git i.e. Cherry Picking

What is Cherry Picking?

Cherry-picking is a method to apply selective commits from one branch to another.

Why Cherry Picking?

→ When working on a project in a DEV environment (say develop branch), suppose five of your team members have merged their features or fixes in the dev.

→ Let us assume only your commit must be moved in the UAT environment which is also in dev.

→ Here are two approaches we can take —

a. manually code again checking the Pull Request or Merge Request (PR, MR, etc.)

b. use cherry picking to do the same fast.

Cherry Pick Commands:

STEP 1: Go to the target branch where you want to apply the commit

git checkout <target_branch>
eg. git checkout UAT

STEP 2: Take out the Commit ID (SHA) that you need to apply. We will do step-by-step in the demo below

git log
9ce4a71716f50b3d618697c57a00fcb9be6eb4e5
987bcad67f05203cc5440958ff0839b5f60c6765

STEP 3: Cherry pick command

git cherry-pick <commit-hash>  // Single commit to apply
git cherry-pick 9ce4a71716f50b3d618697c57a00fcb9be6eb4e5

git cherry-pick <commit-hash-1> <commit-hash-2> // Multiple commits to apply
git cherry-pick 9ce4a71716f50b3d618697c57a00fcb9be6eb4e5 987bcad67f05203cc5440958ff0839b5f60c6765

STEP 4: Resolve Conflicts (if any): If the changes in the cherry-picked commit conflict with changes in your current branch.

STEP 5: Commit the Cherry Pick

git commit        // Nothing to add after commit

STEP 6: Pushing to repository:

git push origin <target-branch>
git push origin UAT

→ This is usually the end of cherry-picking

STEP 7: Now what I had to do recently was cherry-pick@#@#!@@ three sets of Pull Requests. Now the next time we do cherry picking it will throw an error.

→ We have to run the below commands:

git cherry-pick --quit       // When we have pushed 1st set of cherry pick

git cherry-pick --continue // This is when we enterted cherry pick command before pushing

git cherry-pick --abort // When error is not stopping, will return to normal state

STEP 8: Restart from STEP 2 with a new set of Commits to apply.

Project Demo — An IMDB-like UI

→ Let us first see the project in the UAT and DEV environment:

  1. UAT: This is the target branch. All cherry-pick changes will go here.

2. DEV: This is the branch which has a number of features. Some need to be moved and some are not.

→ Let us explore the features that we will cherry-pick:

Feature 1: Dark Mode (Need to go in UAT ✅)

Feature 2: Search Movie (Need to go in UAT ✅)

Feature 3: View Modal (Should not go to UAT — broken Feature ⚠️)

→ We will see this at the time of commits picking

Feature 4: Load More Feature (Need to go in UAT ✅)

Starting Cherry Picking:

→ We need to go to the Repository where have pushed features (for this case develop)

→ It can be in Github, Azure DevOps, Gitlab, etc

→ Now to copy the full Commit Hash, click on the copy icon

→ Pasting Commit Hash

→ So now we have all the things in place. Let us nail it.

→ STEP 1: Going to the target branch i.e. UAT

git checkout UAT

→ We observe all our features gone — dark mode, search, load more

STEP 2: We will run the cherry-pick command. Intentionally, let me do it in two parts.

Running 1st three commits:

git cherry-pick 987bcad67f05203cc5440958ff0839b5f60c6765 e4bf0ccd8a85f996a4d3928c2fb888244693309d e8bec156e2b8d7637928c772be47eb56939378a3

→ Got one

→ Getting Merge conflict in dev. We need to fix it first

→ The next step is to run STEP 5 of the above command i.e.

git commit

→ A popup opens, and your commit message text is visible there, nothing to do just verify you are cherry picking right commit and push

Attempt 2: Cherry Picking the last two commits

STEP 1: Before the new instance of cherry-picking, we have to run the below quit command.

git cherry-pick --quit
git cherry-pick ba80e801c8e0edc56368ad52755d0ff6dc9b921a 47bd7734719b2ce93cd17bd123f498d331151bf9

→ Highly recommend doing one commit at a time as merge conflict may come

→ After fixing the merge conflict:

git add <filename>
eg. git add index.html

git commit. // STEP 5 of cherry picking, a new popup opens just read commit message and close tab in editor

→ Now just push into the same branch

git push origin UAT. // Target branch

→ We observe specific feature commits successfully migrated to UAT.

Closing Thoughts

Mostly during new features or fixes development, we take fresh git pull from the dev branch. This is the best way to do it.

Recently I was working on a project and both my teammates and I merged our feature into the dev branch. Due to some reason only one feature we have to push from DEV to UAT.

Cherry Picking saved the day for us. Otherwise, there were three Pull Requests (almost 15 commits for each PR). So instead of me manually coding and seeing the PR. We applied the selective commits to push to UAT 🙂

Thank you for reading till the end 🙌 . If you enjoyed this article or learned something new, support me by clicking the share button below to reach more people and/or give me a follow on Twitter and subscribe Happy Learnings !! to see some other tips, articles, and things I learn about and share there.

--

--

Amir Mustafa

JavaScript Specialist | Consultant | YouTuber 🎬. | AWS ☁️ | Docker 🐳 | Digital Nomad | Human. Connect with me on https://www.linkedin.com/in/amirmustafa1/