In this lecture, I would like to talk about cherry pick operation and cherry pick allows you to take
any commit and insert it into currently checked out branch as a last commit and you can use cherry pick
operation in several scenarios.
For example, you are working on separate feature branch and have made several commits there and you
want to take just one commit of that feature branch and insert into, for example, master branch or
release branch like a bug fix or something else.
And you are able to do that with cherry pick operation.
Another scenario, for example, you have moved to detached head state and moved 1 or 2 commits there,
but you don't want to create new feature branch and afterwards merge it into release or master branch.
You just want to take one or both commits from detached head state and afterwards insert them into master
or release branch.
You are able to do this also using cherry pick operation.
Okay, let's have a look at cherry pick in action and let's do following.
First, let's create temporal branch called temp.
Afterwards, let's make some changes in that branch, commit them and then return back to dev branch
and cherry pick commit made in temp branch.
Okay, let's create new branch git checkout dash B and in double quotes write temp.
I'll create temp branch and immediately checkout it.
Now I'm in a new temp branch.
Now let's go to VS code where I have already opened this repository and let's make some changes and
let me adjust for example Readme file and let's assume that I want to modify width of this image and
let's make it 200 pixels.
Let's save changes, let's commit changes.
Git commit, dash a dash m.
Some changes.
Of the width like so and changes were committed.
Git log and here is this commit in temp branch.
But let's assume that I don't want to merge this temp branch into dev branch.
Instead I simply want to take this commit and insert it into dev branch.
For that I need Sha1 hash or part of it of this commit.
Let me copy it and return back to dev branch.
Git checkout dev.
Let's clear our terminal here and here.
Type git cherry pick and paste sha1 hash that I have just copied from temp branch.
Let's press enter.
And now you see that new commit was automatically created in dev branch.
And if I'll have a look at history here, you'll see this commit same as was made in temp branch.
But of course here Sha1 hash will be different now because git has created a brand new commit object.
It has another date of creation and that's why Sha1 hash of this commit object is completely different,
but that's how you're able pretty fast and easy take commit from any other place and insert into currently
checked out branch.
And again, notice that this operation is not destructive.
It simply allows you to apply any other changes pretty fast and easy.
You are also able to pass additional options to cherry pick command like dash dash, no commit.
Let's try that.
Let's go back to temp branch.
Git checkout temp and make some changes.
Again, let's go back to VS code and let's adjust this sweets once again.
300 pixels.
Let's commit change.
Git commit dash a dash m change of the width.
In the temp branch.
Let's commit this and let's have a look at the Sha1 hash of this commit object.
This one let me copy it and return back to dev branch.
Git checkout dev.
And here let's cherry pick but with option dash.
Dash no commit.
Let's clear terminal.
Git cherry pick dash dash.
No commit and paste sha1 hash like so.
And now you see that there are changes that were not yet committed.
Let's have a look at status of git repository.
Git status.
And you see that file readme.md was modified and we are able to actually commit those changes using
new commit message.
But before committing, let's actually have a look at changes in readme.md file.
For that I can enter git status with verbose option and you'll see that this change was actually made.
We have adjusted the width here in this image tag and that's change that we have made in vs code in
temp branch.
But as you see with dash dash no commit option, you are able to commit yourself adding a new commit
message if you want and let's do that.
Git commit m changed image width like so let's commit it.
And now I'll see new commit here in history with this commit message.
But changes are exactly the same as we have made in temp branch and we can prove that by entering git
show.
Actually, let me copy.
Sha1 hash of this commit object.
And clear terminal and enter Git show and paste this hash.
And here below you see this change.
Okay.
That's how you are able to use cherry pick operation.
And again, it is useful when you want to take just one commit from any other branch or for example,
detached head state and apply it to currently checked out branch.
Okay, that's all for this lecture.
And in the next one let me talk about rev log command.
Git rev log.
I'll see you soon.
Bye bye.