Git: Understanding cherry-pick

Have you ever mixed up your commits and pushed them to the wrong branch? Keep reading 😄

Onejohi
3 min readNov 27, 2019

--

Git makes most developers flinch, sometimes it’s messy if you have no idea what you’re doing and often a lot of developers get caught tangled up in some mess (me included). In this tutorial, we’ll discuss unfucking your commits after wrongly pushing some commits to the wrong branch…

Scenario.

Imagine you’re working on a feature and a bug pops up or the lead developer requests some changes. You quickly make the changes, commit and then continue with your feature. Your feature branch now looks like this…

After pushing your code to GitHub or BitBucket, you painfully realize you made a bug fix inside your feature branch and wanna take it out and create a branch for it, and a different PR.

Cherry-picking

Cherry-picking lets you pick a commit using its commit sha, and applying that commit to the current HEAD. This makes it useful for undoing changes, however, it’s noteworthy to remember that cherry-picking will NOT delete the commit that was cherry-picked from the feature branch, so you will have to create a new feature branch and cherry-pick all your commits once more without the bug fix. Failure to this, cherry-picking can cause duplicate commits and users are encouraged to use git merge in scenarios where it is risky.

PS: The branch you cherry-pick from should be deleted, cherry-pick the commits into two or more different branches then delete the faulty branch to avoid code duplication.

How to cherry-pick

Pretty simple really. Let’s just use the above scenario. What you should do in this scenario is either cherry-pick the bug fix directly into the branch develop, or create a new branch to cherry-pick into so you can create a new PR for review. Using the latter scenario, we’ll create a new branch and switch to that branch so that it becomes our current HEAD.

git branch bug-fix
git checkout bug-fix

Confirm the commit sha of the code you wanna cherry-pick, use git log to view your commit history. Your commit sha will normally appear like this, I underlined it for easier identification.

To cherry-pick this specific commit into our newly created branch, we will use the command…

git cherry-pick b26ca1fd8862bb05191291e969cf3e463b318e12 --no-commit

With this command, you’ll have successfully picked a commit from one branch, and pasted it to another. The --no-commit flag is optional and you can use it to avoid re-writing your commit messages or being requested to commit every cherry-pick if you’re picking multiple commits.

Summary.

Cherry-picking is a powerful command that can lead to issues if misused without proper knowledge of what could happen. But it can save your life (or at least your day job) when you mess up and include commits into wrong branches remotely.

To learn more about Cherry-picking, check out Atlassian’s guide. It’s super helpful, simple and straight forward.

--

--

Onejohi

Creative 🚀 | Gamer 🎮 | Web & App developer 💻📱 | Graphics Designer 📏📝 | Entrepreneur 💶 | Cool AF 😎🤓 https://onejohi.com | WhatsApp +254727321766