Git CHERRY PICK Tutorial

00:04:41
https://www.youtube.com/watch?v=i657Bg_HAWI

Resumen

TLDRIn this tutorial video, Jack from The Modern Coder explains the 'git cherry-pick' command, which allows users to select specific commits from one branch and apply them to another. Cherry-picking is useful for integrating specific changes without merging entire branches. Jack provides a step-by-step guide on how to find commit hashes using 'git log', apply single or multiple commits with cherry-pick, and discusses potential merge conflicts that may arise during the process, offering insights into resolving them. The tutorial includes visual animations to enhance understanding.

Para llevar

  • 🍒 Cherry-pick allows applying specific commits from one branch to another.
  • 🔍 Use 'git log' to find commit hashes for cherry-picking.
  • 📂 Ensure the target branch is checked out before executing the command.
  • 💻 Cherry-picking can retrieve single or multiple commits at once.
  • ⚖️ Conflicts may occur if changes overlap with existing work on the target branch.

Cronología

  • 00:00:00 - 00:04:41

    In this introduction, Jack explains that he will cover the highly requested topic of using git cherry pick. Cherry picking allows users to select specific commits from one branch and apply them to another branch, focusing on making selective changes rather than handling entire branches like merge or rebase. He sets the context by demonstrating this process with a web application project he's working on with a colleague, indicating the importance of cherry picking for efficiently managing code changes.

Mapa mental

Vídeo de preguntas y respuestas

  • What is cherry-picking in Git?

    Cherry-picking in Git means selecting a specific commit from one branch and applying its changes to another branch.

  • How do I identify the commit hash for cherry-picking?

    You can identify the commit hash by using the 'git log' command followed by the branch name.

  • Can I cherry-pick multiple commits at once?

    Yes, you can cherry-pick multiple commits by specifying their hashes in the cherry-pick command.

  • What happens if there are merge conflicts during cherry-pick?

    If merge conflicts occur, you will need to resolve them before the cherry-pick completes.

  • How do I fix merge conflicts that arise from cherry-picking?

    Refer to the follow-up video where Jack explains how to deal with merge conflicts.

Ver más resúmenes de vídeos

Obtén acceso instantáneo a resúmenes gratuitos de vídeos de YouTube gracias a la IA.
Subtítulos
en
Desplazamiento automático:
  • 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!
Etiquetas
  • Git
  • Cherry Pick
  • Version Control
  • Commit Hash
  • Merge Conflicts
  • Software Development
  • Git Commands
  • Coding Tutorial
  • Programming
  • Team Collaboration