Git Lab Exercise & Assignment: Git cherry-pick: Part – 12


git-cherry-pick – Apply the changes introduced by some existing commits. Cherry picking in git means to choose a commit from one branch and apply it onto another. This is in contrast with other ways such as merge and rebase which normally applies many commits onto a another branch.

In another word, Cherry picking in Git is designed to apply some commit from one branch into another branch. It can be done if you eg. made a mistake and committed a change into wrong branch, but do not want to merge the whole branch. You can just eg. revert the commit and cherry-pick it on another branch.


Make sure you are on the branch you want apply the commit to.


$ git checkout master
Execute the following:


$ git cherry-pick 

#In this case, 62ecb3 is the cherry and you want to pick it!
$ git checkout master
$ git cherry-pick 62ecb3
Cherry picking a range of commits

In some cases picking one single commit is not enough. You need, let’s say three consecutive commits. cherry-pick is not the right tool for this. rebase is. From the previous example, you’d want commit 76cada and 62ecb3 in master. The flow is to first create a new branch from feature at the last commit you want, in this case 62ecb3.


$ git checkout -b newbranch 62ecb3
Next up, you rebase the newbranch commit --onto master. The 76cada^ indicates that you want to start from that specific commit.


$ git rebase --onto master 76cada^
# The result is that commits 76cada through 62ecb3 are applied to master.
Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x