00:00:00
Hey everybody it's Jack for The Modern Coder and
I'm back again with another git video and this
00:00:04
time it is a highly requested one and that is
how to use git cherry pick. In this video I'm
00:00:09
going to be showing the actual commands required
plus live animations right above my head of what's
00:00:13
happening to the get tree under the hood while all
of the commands are being issued. Now I guarantee
00:00:18
this will not only demystify cherry pick for
you, but this tutorial will stick in your mind,
00:00:22
and if you don't believe me ask the thousands of
other folks who have benefited from these types
00:00:27
of explanations in my other git videos. Let's
do it! Cherry picking means choosing a commit
00:00:32
from one branch and applying it to another. To
be more exact, cherry pick applies the changes
00:00:37
introduced by one or more existing commits onto
another branch. Cherry pick is similar to git
00:00:42
merge and git rebase, but rather than taking an
entire Branch worth of commits, or sort of moving
00:00:48
commits around like you would do with merge or
rebase, cherry pick allows you to apply only the
00:00:53
changes that you want to apply. All right now
let's talk about cherry picking in practice and
00:00:57
the actual commands needed to get it done. So
let's say I'm working on a web application with
00:01:01
another engineer, I have a repository checked out
and it looks like this. As you can see that white
00:01:06
branch is our mainline and the blue is another
branch that my co-worker started to work on
00:01:10
the navigation. If I populate the actual commit
messages you can see that my co-worker actually
00:01:14
fixed a bug on their branch before starting to
work on the navigational components. I'm working
00:01:20
on mainline and I want to grab that bug fix
without taking any other other unfinished changes,
00:01:24
now this is where cherry pick comes in and is
the right tool. In order to use cherry pick you
00:01:28
need to refer to commits by their commit hash,
or in other words the unique ID of the commit
00:01:33
that you want to cherry pick. Now to get those
actual unique identifiers I can run 'git log'.
00:01:38
If I just run a regular 'git log' it's just going
to show me the commits from the branch I'm on,
00:01:42
now I want to see the commits from my co-workers
branch so I'm just going to run 'git log' and
00:01:47
specify the branch I want to see commits for (and
I'm actually going to use the '--oneline' option
00:01:52
just to make it a little bit more readable. Now
here are all the commits on the nav branch. On
00:01:57
the left is going to be the unique identifying
hashes of each commit and you can see the bug
00:02:01
fix commit is right here. Now I want to pull that
on so I'm going to go ahead and copy that hash.
00:02:07
Alright, with that hash code we have all that
we need to run cherry pick. Now you want to make
00:02:12
sure that you have the branch that you want that
cherry picked commit to move onto to be checked
00:02:16
out. So you can see I have main checked out, it's
in my prompt but you can also run 'git status'
00:02:21
and you'll be able to see what branch you're
on. If you need to you can just get check out
00:02:26
your branch. Okay so to run cherry prick we're
gonna run 'git cherry-pick' followed by that
00:02:32
hash code of the commit that we want to grab.
Now when I run this command what git is actually
00:02:37
doing is it's applying the changes introduced by
that bug fix commit onto the mainline branch and
00:02:42
actually creating a new commit with all those
changes. You can see this if I run 'git log'
00:02:49
and compare that with the hash code of the
nav branch's bug fix commit that they are
00:02:55
different hashes. But wait! What about multiple
commits? Well cherry pick can do that too,
00:03:00
so let's roll it back and say I not only want
to grab that bug fix, but we can see that my
00:03:05
co-worker added a new logo for the site. Now if
I wanted to grab both of those without taking
00:03:09
any of those other in progress changes I can do
that with cherry pick as well and the process
00:03:14
is similar. So to cherry pick multiple commits
we're just going to need the unique identifier
00:03:18
of each of those commit hashes. So what I'm going
to do is I'm going to run 'git log' and I'm going
00:03:24
to figure out those hashes from the nav branch.
Now you can see here the logo is in this commit,
00:03:31
and then the bug fix is in this one, so I'm
just going to grab both of those hash codes.
00:03:36
Run 'git cherry-pick' and then grab those two
hashes and enter. What's going on under the hood
00:03:44
is git is applying the changes introduced by those
commits one at a time in the order that they were
00:03:48
specified in the cherry pick command. So in this
case, I added the hash of the logo change first,
00:03:55
followed by the hash of the bug fix commit second,
so git is going to apply those in that order. And
00:04:01
at the end of the day we're going to end up
with actually two new commits that contain
00:04:04
those changes. If the changes introduced in those
commits that were cherry picked have conflicts
00:04:09
with what I already have on mainline, or if I
was already working on those files that were
00:04:13
also touched by that bug fix commit, I'm going to
have a problem I'm going to have a merge conflict.
00:04:18
I know that can get a little bit confusing but I'm
going to show you how to fix those right in this
00:04:22
video. And don't worry, the video gets straight to
the point the only reason I separated the two of
00:04:26
these videos is just because if some people are
looking for actual advice on how to fix merge
00:04:31
conflicts and cherry pick, I didn't want to have
to have them watch this whole video just to get
00:04:34
to that part. In any case, click that video and
I'll explain how to deal with any merge conflict
00:04:37
that could come up while you're using cherry
pick. Thanks for watching I'll see you later!