016 Introduction to Git cherry-pick

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everybody this is Dan once again with another get tutorial video today I'm going to talk about git cherry-pick so let's go ahead and get started I'm going to be using this HelloWorld repo that I've used in many many videos there's not a whole lot going on in it and I'm actually going to be using two terminals today so if you notice in the top here I am Dan at local hosts and I'm going to be Bob on another machine called Willow over here so I believe switching back and forth so I got the colors a little different so hopefully it's easier to follow but I'm going to show you an example of using git cherry-pick one example of when to use it and one example when not to use it and before I get into that if we just look briefly at the man page you might be wondering well what is get cherry-pick it's almost exactly what it sounds like it's basically applying the changes introduced by existing commits onto a branch that you have and so this is often used for things like hot fixes if you you know if you pull some stuff and and your current state of your repo is broken and somebody else says oh yeah I found that I have the fix for it it's in this commit get cherry-pick it so you can continue so we're actually going to show that as a first example so let's go ahead and see what branch were on here we're on the master branch and we are going to fetch and merge with the latest state of origin master so we'll go ahead and we'll do a git fetch here and it's actually got a SSH to another machine so it's waiting to authenticate here and I believe I'm up to date but I just wanted to do this for completeness okay so yeah we're definitely up-to-date so let's go ahead and merge with Georgia master and it was fast-forward merge which is awesome so now if we do make in our project we find that we have an error and so this is the setup this is though we're pretending this as an example and this happens a lot in actual software development projects where accidentally somebody pushed something that is broken so as a developer we can now try to track it down and fix it ourselves but let's pretend that we got an email or a call from Bob where we talked to them and basically Bob who is a developer on this project - already found this error and fixed it in a commit and so what we want to do is cherry-pick that fix onto our branch and so what we have to do then is let's look at our get remotes we should have a bob remote and we do and so let's make sure we have all the refs for Bob by doing a get fetch on that remote and so you know we have to do this - if we hadn't fetched recently we need to do this to make sure we we have Bob's latest state so that we can cherry-pick the commit that he created okay and you can see that it brought in a couple branches and Bob told us that he fixed this on the master branch right so we want to look at the get log of this guy see if we can find this commit he's talking about okay and it happens to be at the top check it out so in order to get cherry-pick all you really need is the sha-1 for the commit that you want to cherry-pick and so that's why I've highlighted it and copied it here so it's 6dc so we're still Dan in our repo and we are on the master branch so to get cherry-pick all we have to do is get terrific and then the sha-1 that we want and what happened when we did this was get took the content from this commit which you can you can sort of visualize it as the diff associated with this commit the new content that came in and it merged it into the current state of our current branch which is master so if we look at our git log now we're going to see hotfix for duplicate parameter right that makes sense and the author of this was Bob now one thing that's interesting to note here that if you're new to get you might not realize or understand that remember we get cherry picked the six uh what was that it was a sixty see commit right well in our branch now check it out we have the same content but with a different sha-1 so the sha-1 is different because get cherry-pick creates a new commit with the same content and it actually says that in the man page if you look at it so as you can imagine this can cause strange merge conflicts if the content is cherry picked and then modified in two separate lines of development so that's just something to be aware of and I might talk about the implications of merging and conflicts of this later on but for right now don't don't really worry about it so we have cherry pick to the content right and so if I do a make now I will find that my project is working again and if I do a run I should always do that too right so let's run hello and it's also running so cherry picks are most useful for this concept of a hotfix where you have a problem that is impeding your work in one way or another and somebody else has already fixed it so yeah I want to show an example of using cherry picks incorrectly because some people may you know it's kind of a cool concept and people are like well you know if you're developing with ten people and someone creates some content and you want that content but you don't want to merge with them for one reason or another that you might be tempted to cherry-pick it and so I'm going to go through an example here where we're going to switch over to the Bob terminal and he has his hello world repo and as you can see he's on a branch called sphere attempt and if we look at the git log he has attempted working on a sphere class and he's one commit deep from his hotfix on the master branch so in this pretend scenario Bob hears from Dan that Dan has already created some sphere content and that exists in Dan's sphere class so since that's already set up as a remote for Bob we'll see that get branch - a shows our current branch and then also Dan's fear branch and so if we look at Dan sphere branch look at the git log we'll see he's made one commit here and now remember and I almost forgot here let's go ahead and do a git fetch make sure we're up-to-date with Dan's sphere branch so we're going to go ahead and do a git fetch on Dan which as you know will download the latest refs from Dan so that we can see them although it does not affect your working tree okay so it did actually bring in some new content on Dan slash master and Dan slash sphere so if we do a git log on Dan slash sphere now we'll see oh it looks like there's two commits here right so you know just by observation we can see well there's two commits here you might ask yourself hey if I want to if I'm Bob and I feel like cherry-picking the sphere content which I'm going to explain to you why is a bad idea you might ask yourself well how do I cherry-pick multiple commits what do i do I just take the latest one do I take the first one do I have to do both and the answer is you really need to if you have to do this you want to cherry-pick from the oldest to the newest as long as they're linear commit so in this example if we wanted to do that if branch we're on our sphere attempt so the plan here would be to get cherry pick the old commit and then the new commit so let's give it a shot and let's see where it goes with it share to be pic to e1 and we get this error which I wanted to show you could not apply to e1 at it's for your class not fully implemented which is the name of the commit or the commit message it says we have basically merge conflict so we have to resolve them so if we do get status now you'll notice that we have unmerged paths so I wanted to point this out because you may think if you're new to get that you can only get a merge conflict on a merge action but get cherry-pick is a merge action so that's just something to note and basically what happened here is Bob had done some development and he made a file called sphere CPP and sphere HH and he started implementing it so if we do a git merge tool we'll see all those differences but and so I guess what I want to show you here is that you have to resolve these merge conflicts and then you'd have to cherry-pick the second commit that we were talking about so it kind of seems like a lot of work and so it if your experience with git you're saying why are you doing this and you're right that this is not the correct way to go about getting Dan's sphere changes I mean after all if you talk to him and he tells you it's fully implemented why would you want to cherry-pick that content when yours is in progress you know it's going to create a merge conflict right so let's go ahead and abort this by doing a reset hard and so now we are back at the sphere temp branch and so this is just an example of how not to do a cherry-pick and so what we should do instead is go back to the common base which we know is master because we saw it in the git log create a new branch for our content we'll just call it spear and then get merge with Dan slash spear and so we get the merge commit but no conflict because we were at a branch state that didn't have our work in progress when we did the merge and we do a git log now and we see that we have the merge the merge commit here the hotfix that was on the branch and then the sphere content commit here and here and so the reason they don't show up in order is because of the way get log sort of collapses merged history into a linear timeline so I'm not going to go into that into this video but I just wanted to show you that so I was doing some reading and get cherry-pick actually will let you use a range of commits and so in the case where you need to cherry-pick multiple commits you'll probably want to investigate that I might do a video on that but I wanted to keep this one short just as an introduction to what git cherry-pick is and what it can do for you so with that I think I'm just going to wrap it up and so just to summarize to use git cherry-pick you basically want to do a git log on the hotfix reference which is usually a branch that a co-worker told you or they may email you the exact sha-1 of the commit that you need to find the commit and then you want to do get cherry pick that sha-1 on your current branch it will apply applied to current branch okay so and that's pretty much it so happy cherry picking my name is Dan I'll see you guys next time
Info
Channel: Dan Gitschooldude
Views: 60,887
Rating: 4.7317553 out of 5
Keywords: git, software, tutorial, linux
Id: -ndmel-4wsk
Channel Id: undefined
Length: 13min 15sec (795 seconds)
Published: Sun Dec 20 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.