Resolve Git Cherry Pick Merge Conflicts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Hey everybody it's Jack from The Modern Coder  and today I'm going to explain how to solve merge   conflicts that come up when using git cherry pick.  There are two main types of merge conflicts that   typically come up when using cherry pick, and I'll  explain both of those. As always I'll be using   live animations of what's happening under the hood  while I'm issuing these commands, and you'll find   those animations pop up right above my head in  this video. I promise these animations really   help and I'm confident by the end of this video  you will not only understand how to fix any merge   conflicts you come across, but you'll be able to  tackle them without any fear. Alright let's not   waste any time. Let's jump right into it! Here's  what my repository looks like at the moment:   that white branch is mainline and then you can  see my co-worker split off onto that blue branch   to work on some navigational components. But as  you can see they've also fixed a bug before they   started working on that nav component, and let's  say I want to take just that bug fix and pull it   over into mainline without taking any of their  other in progress changes. This is what cherry   pick is really good for. If I were to start that  cherry picking process by going ahead and grabbing   the commit hash of the bug fix and attempting  to cherry pick you're going to see that I have a   problem, I have a merge conflict. Well how do you  know what failed? Well the failure message on the   screen doesn't tell us that much so let's clear  the screen and run 'git status' to get a bit more   information. Okay this is much better. What this  is telling us is that we are currently on main.   We're also currently in the process  of picking this specific commit,   and it also gives us a little cheatsheet about how  to proceed in the case that we need a reminder of   what our options are from here. Lastly it tells  us which files it had trouble merging, and that's   the key part. As you can see here it tells us that  both the bug fix commit and the latest commit on   main modified config.yaml and it couldn't merge  it automatically. If I pull up our git tree and   zoom in a little bit on these commits you can see  that the bug fix commit actually fixed a bug in   config.yaml file; however since splitting off,  main has also gotten a new commit that modified   config.yaml as well and that's why there's a  merge conflict. Okay let's fix it. First off,   if you realize you've gotten in over your head and  you just want to eject from the situation and you   don't want to do the cherry pick anymore you can  always type 'git cherry-pick --abort' like this   and git will cancel your cherry pick and set  everything back to normal. Okay let's just roll   it back and say we do actually want to continue  to fix this conflict. We need to open up the   file that has the conflict and manually fix it. In  this case the file is config.yaml and so I'll just   open it for editing in VI. And I'm just using Vim  because it's available on most terminal systems,   but you can use any text editor that you  want whether you use Sublime, VS Code,   JetBrains whatever. You want to look for these  weird looking lines with the less than and greater   than characters, this is the specific spot in  the file that git couldn't merge automatically.   As you can see: that top section indicates what  that part of the file looks like on main, and   below that you can see what that part of the file  looks like on the bug fix branch. Your goal is to   manually replace this entire block with what those  lines should look like after the merge: there may   be one or more of these blocks. So as you can  see here that "show_drafts" moving from "true"   to "false" is the bug fix so I want to keep that.  I'll just press "I" in Vim to enter edit mode and   change that to "false". Then on mainline looks  like the new changes are the addition of these   social media handles so we can keep those. Then if  I go ahead and delete the rest of the lines that   we don't need this is what the file should look  like after the merge. That's it! You can make any   changes you want, but for now that's all I need.  For Vim the command to exit and save the file is   'esc:wq' so I'm going to go ahead and enter that.  I'm popped back out onto my main terminal. Now   that we've made that merge manually, we can go  ahead and stage that file by using 'git add'.   We can go ahead and verify this by typing 'git  status' again, and you can see it turns green   that they're waiting to be committed. From  here it's really easy, all we have to do is   type 'git cherry-pick --continue' for it  to continue the cherry picking operation.   One last thing before we're done, git is going to  kick us into our terminal's default text editor to   specify a commit message. This is good practice to  give descriptive names to our commits especially   if they were cherry pick. So it ported over the  original commit message here so I'll add "bug fix   in config", I'll also say "added socials". Then  we're going to make sure to let it know that this   was cherry picked from this particular commit.  Then I'm going to save it, and again it's 'esc:wq'   if you're using Vim, hit enter and that's it! Now  if I clear the screen and run 'git log --oneline',   you can see I end up with that cherry picked  commit right on top: that includes our merge. I promised there were two types of merge conflicts  that you could potentially run across with cherry   pick, and the second type of conflict is  going to happen when those files on main   aren't committed yet. So in other words they're  just your local changes. Now if that's the case,   when you run 'git cherry-pick' you're going  to end up with a message like this - slightly   different than the last message that we saw.  What git is telling you here is that you have   local changes that conflict with the cherry  pick that you're trying to do and it's just   going to abort altogether. Now you can either  commit those changes into a commit on main and   go through that same workflow we just talked  about OR you could just also set aside those   local changes using 'git stash' and push them to  the side then continue with your cherry pick as   you did before. That's going to work, but when  you go to pull those changes out of your stash   you're going to get a merge  conflict that you have to solve,   so either way you're on a  collision course with merge. I'll have a video out about git stashing  shortly because the workflow is pretty cool,   but if you want to use it, fixing a git stash  apply merge conflict is very similar to what   you just learned in this video so I'm sure you  could figure it out. If you want to learn more,   and other git techniques with this  same sort of video animation style,   I have a whole playlist and multiple other videos  about that. Subscribe if you want more git videos,   then let me know in the comments if there's  anything else you'd like me to cover in this   way. Other than that I think that's all I got for  now, thanks for watching and I'll see you later!
Info
Channel: The Modern Coder
Views: 8,673
Rating: undefined out of 5
Keywords: programming, linux, unix, terminal, git
Id: aUeNbpSkY8k
Channel Id: undefined
Length: 5min 52sec (352 seconds)
Published: Mon Dec 19 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.