Git cherry-pick

The cherry-pick command is used to apply a commit from one branch to another branch.

Let’s say you have a feature branch that you are working on that have two stories. You finished the two stories but only need to push the first story to master. You have one commit for story number one and another commit for story number two. Note here that you can’t use merge because merge will bring all the changes(story one and two together). We don’t want that we only need the first story code. Here you can cherry-pick the commit for story number one to master.

Steps
1. Find commit id of the commit you need to cherry-pick
2. Checkout the branch you want to apply the commit to
3. git cherry-pick commit-id

git checkout master
git cherry-pick 1232349F

Here is another example. Let’s say you are at branch (F-G-H) and you want the change that’s made in E. To do this you need to cherry-pick E. Note that these notes are commit ids.

git cherry-pick E

If you want to cherry-pick more than one commits then do this

git cherry-pick commit-id-1 commit-id-2 commit-id-3

Cherry pick all commits of a specific branch

When you want to cherry pick all commits of a specific branch but not commits merged or cherry-picked into it. You can just cherry pick the whole branch. Maybe there are so many commits that it does not make sense to specify each commit.

Let say you have a branch call color, css, and grid. You working on the css branch but have changes from color branch that you don’t want to push to the grid branch. What you need to do is cherry pick all commits from the css branch to the grid branch. Here is how you do that.

git checkout css
// make changes
// commit and push(abc)


git merge color
// merged 1 commit(cde) from color to css(the working branch)

// make changes again
// commit and push(fhg)

// Cherry pick all of css commits(without color commits) to grid
git checkout grid

git cherry-pick css
// you have commits abc and fhg but not cde.

 




Subscribe To Our Newsletter
You will receive our latest post and tutorial.
Thank you for subscribing!

required
required


Leave a Reply

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