- [Cameron] Hi, I'm Cameron McKenzie,
I'm the editor and chief over at the serverside.com.
And, I wanted to talk to you a little bit about how
to git cherry-pick a commit.
Now, I've got a folder here with no files in it,
I can prove that by doing a little listing command.
I'm just gonna initialize a git repository,
and into this folder,
I'm gonna create five files, one at a time.
So, I'm gonna that file named alpha,
and I'm gonna add it and commit it.
And then, I'm gonna add another one named beta,
and notice that every time I commit it,
I provide the commit number
and the number of files that are part of the commit.
So, here you see I've got a second commit for two files.
And, I'll create a new file called charlie,
and do a third commit there.
And, you can see these files being created on the fly.
I'm gonna create a fourth file,
named whip it,
and I will commit that fourth file as well,
you can see that added there.
And then finally, I'm gonna use the echo command
to create one more file.
And then, commit that last file.
And as you can see,
I've now got five files,
one two three four five,
and I should have five commits as well,
oh and look at that,
I didn't do the commit command properly there,
I need a dash m,
and that looks good.
I now have five commits,
I think a reflog
could even prove that.
Five commits, five files,
four three two and one.
Okay, now, what would happen
if I deleted all of these files?
Okay, and then git commit.
So now, I've deleted all of those files,
I've added the deleted files to the index
and then committed that.
I now have five commits there,
well maybe actually a sixth,
and I have no files in my repository.
So, the question that I would pose to anybody right now is,
what would happen
if I did a git cherry-pick on this third commit?
So, if I cherry-pick the third commit,
that's where there were three files,
would I end up with three files in my folder,
cause I have none now?
Would I end up with two files in my folder?
Would I get everything up to and until that third commit,
but not those files included in that third commit?
Or, would I get only one file,
that is that one file
that was associated with that third commit?
Well, it's time to see,
the command to do that would be git cherry-pick,
and then the number of the commit.
I could probably use head two,
but I'll do 34946d4.
All of a sudden,
I now have that file,
that commit now brought back,
and you'll notice that
I only end up with one file in my folder,
charlie.html.
And so as you can see,
the cherry-picking,
it doesn't take you back
to the state that the repository was in
at the point in time of the cherry-pick that you committed,
that was committed,
all it does is actually bring back the individual files,
or the individual changes
that are associated with that one commit.
Now, of course, I did this one file at a time,
you may have a particular commit where
multiple files were added,
multiple files were deleted,
and multiple files were edited.
The point is,
if you cherry-pick that commit,
it will bring those changes back,
but it won't bring other changes
that may have previously existed.
So, it only brings back
the changes that were associated with the commit
that is being cherry-picked.
And so, that catches a lot of people off guard,
some people think that the behavior is gonna be more like
a git reset, but it's not.
A git commit only targets the files
that were associated with the commit
that you're cherry-picking.
Now the other thing that's worth mentioning,
is if I do a git reflog,
you'll notice that I actually got a brand new commit,
added to my repo,
added to my git history,
and it now it says git cherry-pick third commit three files.
And so, you might not have noticed that,
but the git cherry-pick command automatically does that.
It automatically registers a commit,
which might be different from
some of the other git commands you might use
to manipulate files in your git history.
Anyways, that's it,
if you wanna learn more,
you can head over to TheServerSide,
we do have a full article on that,
how to git cherry-pick a commit,
and we even have one on how to
git cherry-pick from another branch.
And, if you wanna know more about me,
and what I'm up to,
you can follow me on Twitter,
@cameronmcnz,
and then you can even get all of my files
over on GitHub.