Welcome in this video, we'll talk about cherry picking and how we can use it to bring in a specific
commit from another branch into our workflow and how we can do this with and without committing, we
can bring the change in as staged or as a completely new commit.
Let's talk about this now.
Let's say we have a main branch and our feature branch has a few comments and we're interested in one
of these, but not all of them.
Let's say we'd like F1 merge in to our main branch.
Now we could do a merge or a rebase pulling in all three of these commits, but say we only want this
one.
Well, we can get this comment by cherry picking running.
Get cherry pick, followed by the commit ID of a commit in another branch will result in pulling that
specific comment into our branch without merging the entire branch.
Let's see how this works now.
From here in first repo, I'm first going to run Git Branch.
Now I see that Maine is the only branch.
I'm going to create a feature branch.
So let's run Git Checkout Dash B feature.
Now that I'm here on the new feature branch, I can make some new commands.
Now I have F1, F2 and F3.
I'm going to make commits to each of these, each in their own commit.
Starting with F1, so I'm going to open F1 first and I'm going to add some text here.
I'm going to say Cherry pick this commit.
And save and close next, I can make a commit from this, so I'm going to run, get commit dash a dash
am and I'm going to call the C P for cherry pick one.
So there's my first commit next.
I'm going to make a change to F2.2 by opening this and entering.
Don't cherry pick this and save and close.
And I'm going to commit this one.
So I'm going to run Git Commit Dash eight, Dash M CP two.
And finally, I'll make a change to F3.
So I'm going to open this and I'm going to add the tags.
Don't cherry pick this either, and save and close.
And once again, I'll make one last commit that says CP three.
Now, if I run Git Log and I'm going to use Dash Dash pretty one line and I'll see that I have seen
P1, c P2 and CP three.
Now what I'm interested in pulling into main is only this commit here CP one.
So I'm going to copy the last few digits of its commit ID by right clicking and copying them here.
And now I can go back into the main branch by running Git Checkout Main and I can cherry pick this,
so I'm going to run Git Cherry Dash Pink, followed by the community running this.
Now we see that we have a new commit.
I'm going to view the get log again.
And here we are on main and check this out.
I have CP one.
And of course, I can open f one and I see that I have the cherry pick this commit text here.
So the cherry pick was successful.
Now I mentioned that we could cherry pick without committing.
This will pull in a commit from that branch and stage it, but stopped just short of committing.
Let's scroll up to where I looked at the commit log here and feature.
Now let's say I'd like to pull in CP two, so I'll take the first few digits of the Commit ID and copy
it now.
The way to pull this in without committing is get cherry, pick dash and for no commit it and then the
Commit ID.
I'm going to run this now if I run get status.
I see that I have modified changes to F2, so we don't have a new commit.
But those changes to F2 from the feature branch are here and I can prove that by opening F2 and I see
don't cherry pick this.
Now this is useful because we have the changes here, but we haven't made a commit so we can still change
the modified files and come in again.
I'm going to remove the word don't and change F2.
So it says Cherry picked this and I'm going to save and close now running get status.
We'll see that F2 has both changes staged for comment and change is not staged.
So we can add this.
I'm going to run Git, Add Dot and now get status will show me that the changes are staged and I can
finally commit this.
So I'm going to run Git commit and I'll simply run this command and this code pops up, allowing me
to edit the commit message.
I'm going to change this to say CP two cherry picked from feature and I can save and close.
And I have my changes committed.
So now that you've seen how to cherry pick, it's your turn to do this on your own.
Cherry pick the CP3 comment from the feature branch without committing open up the file and make another
change to it, then stage and commit.
Now, once you're done, push to the remote and then delete the feature branch.
Pause the video and do this now.
OK, so we can cherry pick that third commit, so I'm going to scroll up to where we show the commit
log here in the feature branch.
And this is CP3.
I'm going to take those first few digits of the commit ID and copy them.
Now I'm going to run, get cherry, pick dash and pasting in, and it looks like I didn't quite paste
what I expected.
I'm going to try this again.
Get Cherry Dash pick.
Dash N.
And there's the commit ID, I'm going to run this, and now I have stage changes so I can open up F3,
edit this and I can change this.
I'm going to remove the donut and change this to say Cherry, pick this two and save and close.
Now I can stage this with kit and dont commit this with Git commit dash em and say Cherry picked this
two and we have our changes staged now.
I can push this to the origin with Kit Push Origin Main, and now I can delete the feature branch.
I'm going to run Git Branch Dash Uppercase D feature and we've deleted the feature branch.
So in summary, we learned about cherry picking and how we can use get cherry pick to pull a comment
in from another branch.
And we saw that we could do this with and without committing.
If we add the dash in option, then we can pull that commit in as a staged change and we can further
change it before we committed.
We now have another tool under our belt when it comes to version controlling and get great job.
I'll see you soon.