Git Rebase vs Merge explained - Computer Stuff They Didn't Teach You #6

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey friends the little boys are asleep so it is time for me to do another get video if you haven't seen before i've been doing a series called computer stuff they didn't teach you they're not all going to be about git but until i get through git i think i'll do a few numbers four and five i think are pretty decent you can take a look at number four get 101 basics i think it's actually a pretty good video and i would encourage you to check it out in video number five which is part two of the get series if this becomes a series i talk about pull requests a lot of folks in the comments said that they wanted to hear about rebasing because we talked about git basics and branching and we talked about pull requests where you pull from someone else's branch and merge it into your branch so i thought we'd try one on rebasing because rebasing is about the scariest thing that people can do in git and people make it a lot more complicated than they need to so let's see if we can make it simpler before we do that let's just get a sense of where we were again i would encourage you to check out the previous two videos but let's take a look i also want to point out that i'll be using the windows terminal and i've been using a thing called oh my posh that has allowed me to set up what i think is quite a lovely prompt here so i've got a couple of things in my prompt i've got my path my name my computer name my my main branch there here's maybe called master mine's called main and i also have by the way my blood sugar because i'm a type 1 diabetic so i like to know what my blood sugar is all the time and i've got an implant that sends it up to the cloud and i bring it down into my prompt so i got all the things that matter to me sitting right there cool so uh actually right here one of the things that i've noticed right off the bat is i've got a a red prompt here and it says 4 with a downward arrow that means that there's something up there in the cloud waiting for me up in github let's go see what's going on up there you'll recall that in the last video we pushed everything up to github so this is kind of the authoritative source for our things ultimately remember remember though you get to decide where the authoritative source is and you'll see here if i look at the commits i had some lovely pull requests from some friends put in some suggestions and some changes so thank you for those for our little learning get here so now i'm going to come back and i'm going to say git pull i'm going to bring those things down that's kind of cool look at this i just fast forwarded and now i'm green so that means that my main on my local machine is now in line directly with the one up in the cloud up there in github it looks like i brought home 44 insertions one deletion and three files got changed so some changes there you see lots of changes in this new markdown file that someone gave us and then somebody was messing around in our in our test dot text file let's go and look at that cool so someone made changes there and i brought them down you'll notice though that there was no conflicts there was no trouble i didn't have to merge anything they just kind of fit nicely because they were adding and they weren't competing with the lines of code that i had already written or at least the lines of text files now let's go back up here and i see that i've actually got one pull request so let's see what that's about okay look at that so it looks like last week our new friend here who is a junior web developer has gone and sent a thank you and they've updated one of my text files look at that that's pretty cool so i'm going to go in here and say let's ship it with our ship at squirrel i love the ship at squirrel he's excited to ship our ship our product and i'm going to say merge actually i could put this swiss ship it's squirrel up here or i can put him down here too put him in two places okay so i've just merged that commit from his universe is branch into mine that's cool hit refresh see there's no more pull requests all right we can see that the very last thing that happened was the merge from me a few seconds ago i can come back down here and i can say git pull and look i've now pulled those down and brought his stuff forward now i'm going to run visual studio code and see what our new friend has to say i probably should have looked at his code before i merged it in i would encourage you to do the same i just watched your youtube videos and i'm learning and updating my knowledge thank you mr hanselman no need to commit this see that's why you should read these things first i didn't do that because when i merge code i do it directly in production cool now i'm in visual studio code which i mentioned of course in the previous videos i am using one extension that you might want to get for yourself and as i recall that was called the get history extension i'll show you why it's nice because if you go over here and you press the get history extension you get this lovely graphical branching thing here that shows you kind of what's happening and this is pretty clean because i'm paying attention to my repository but you'll notice something here people do things in parallel universes right they split the universe they do their thing and i merge it back in but these universes these moments here surya was working on something here maggie was working on something here alston is working on something these things are happening in parallel universes until i bring their history into the main line but those moments those moments in time in those parallel universes those branches they didn't disappear we haven't re-written history we've merged history now this is quite nice because it kind of goes like that but what if many people had been working on their stuff and we had a half dozen parallel universes a whole team of people working on stuff and some people were ahead of others and some people were back in time that can be a little bit overwhelming and people don't like that some people like a nice clean linear history they basically want to deny that those parallel universes ever existed and that's probably okay but it can get a little bit messy if you rewrite history right you go back in time and you kill your grandpa do you exist it doesn't really work in time travel movies sometimes works in git if you know what you're doing one of the other things that's worth thinking about is there are some interesting movies that are time travel movies because i think of git and time travel being really similar where someone changes the past and only one person knows that the past changed and they're like what like lincoln never got shot what happened like no what are you talking about lincoln never got shot what i'm the only person in the world who knows that they rewrote history that can be a problem and get it's sometimes nice to see the work that happened on those branches but let's let's rewrite history let's save lincoln and see and see what happens shall we okay now if you'll recall we were just goofing around we made some text files we're not actually changing any code here now i can do this two ways i can open up a terminal inside a visual studio code i'm in vs right now or i can do this from the windows terminal which i like and i also like that the new windows terminal has this this lovely focus mode that's kind of cool i get to remove everything which is nice okay so let's try to get into a little trouble keeping in mind that i don't really know what i'm doing so i'll probably mess it up now i can make a new branch or i can go and say check out a new branch and let's make make a parallel universe with evil spock in it okay so we're now in a parallel world called mr spock and you can see my log and the other things that are happening in the other world if i go back to visual studio code and i come in here and i hit refresh nothing's happened in this universe you see the origin which is up there in github myself locally and evil spock are all parallel nothing has happened that's special in my in my new universe and my files are the same maybe i can make a new file evil text these are my evil plans and then we'll make another one called pure evil so many more evil plants okay and you'll see when i hit enter there that my prompt indicates that on my branch two items have been added so i'll go and do a git add and git commit all right now let's go look at our history here we see our commit it's in the future right because right now we are ahead of maine we're doing stuff way in the future okay let's do a couple more of those because we're making evil plans my friends look at that what we just did was we didn't add everything and a commit at the same time that saved me a little time i didn't have to do an add and commit i didn't get to pick what i wanted but it was still kind of cool now i've got that one then i say you know maybe i don't like this maybe i don't like that i want to rewrite the last commit maybe that message was wrong so i'm going to go on amend this is a little bit of a oops i jumped ahead of myself no i'm actually not evil okay go back over here notice that that message that we had before it's gone so we've rewritten a moment in time one time i saw this video game called prince of persia and in prince of persia not the original one on commodore 64 but in the more recent one when your person would die they would go and say oh i'm sorry that's not what happened and then they would just do a redo almost as if the game itself was telling a story about what had happened and then when he made a mistake he'd go oh no that's not all what's happened back up so we just kind of did an undo just for a moment and we amended that commit just for a second so that's a little bit of of rewriting but that's not in fact a rebase okay now let's go back over to main we're back on main you'll notice in our world in the in the good world there's no evil there's nothing bad happening over here so we'll make some changes do some stuff smurfs whatever hey friends okay i'm going to go and do a couple of updates good things working on this main branch there's one okay thanks for the compliment cool the lovely compliment okay so i'm making changes you'll know that this is updating letting me know that i can send it back up if i want to now i'm doing this to go here and show you see here it's showing main i want to show all branches now look the main keeps going we're doing our own thing evil is here now we could merge it in we could merge it in i'm going to try a little trick here this is a little experiment i've never done this before what i've got here is a my surface my little surface tablet i plugged it into a little usb dealey so what we'll do is i'm going to take my pen and try something new okay let's look at this we've got our our main world here there's main okay and then we've got uh we've got pure evil parallel universe over here okay now in the main we've got a couple of commits i'll make those commits little little squares and over here in evil we've got a couple of commits as well and their little square looks like one of them nice little recognizes a square and the other one's not this is microsoft whiteboard there we go that's a parallelogram evil parallelogram okay now if we merge these things in theoretically and we have this new reality all these things happened in this order in parallel universes but with rebase what we could do is we could say you know everything evil that stopped around started around here one two three in that order and then these other things happen kind of in parallel over here in another world we could change that we could actually make things very different we're going to literally rewrite history and the way we do it is by rebasing or basing this work here which is currently based on it's started or it's based on that first commit we're going to change that and we're going to actually pick it up we're going to pick up this whole thing here we're going to move it over here in order and we're going to base it on this new reality and then all this goes away and things happen in a different order that branch ceases to exist and the evil has then come into our universe rewritten history and everyone's going to be like wow what happened was that always like that like yeah totally uh why didn't you why did you think it was different that's a good thing or a bad thing it depends on how evil you are doesn't it so let's go see what that looks like over here shall we we'll go and say get checkout go back over to evil evil world and when we do this we're going to do a get rebase we're going to say rebase on top of main we're going to say take the work we've been doing in parallel because other things have been happening in maine maine land it just keeps going and would you just grab all of that and just pop it on the top and basically play it back it's like take that little time loop move it into the future and then play it back as if it had always been that way now sometimes this works sometimes it doesn't because it depends on whether or not people were in those files no one was in the evil dot text files they're a separate thing in a separate branch so i don't think we're going to have any trouble if you're concerned about having any trouble you can say dash i for interactive you can actually pick little moments in time and say that happened that happened and that happened but this other thing didn't happen let's see what that looks like so look at this what happens is it pops up your favorite text editor now mine is visual studio code and this can be a little scary because what's happening is we're actually in the middle look at that it says waiting for your editor to close the file so we have not done the thing yet the the instructions that you give to git happen here you're actually going to put the instructions right there so that's an instruction and the commit if you recall from our 101 is like a hash or an identifier that says that's the thing i did and that's the other thing that i did okay so we want to pick this we want to pick that let's see what the other options are look at this pick that commit ooh look at that squash a commit use this commit and squash it into the previous one that might be kind of cool all right we can run a command or label something do all kinds of stuff the ones that we're really interested in are either pick or maybe squash so let's go and do this remember how we said we were pure evil but we're so evil we want to rewrite history and make it like we're not so instead of pick i'm going to actually say squash and we're going to take these two things and pretend that they're one we're going to squash the whole thing i'm going to hit save i'm going to close this file go back here and it says waiting for your editor to close the file all right and look at this it says hey this is a combination of two commits this is the first commit and this is the second one and actually get rid of that we're rewriting history and we don't need them to know uh nothing to see here we also like smurfs cool all right successfully rebased evil spock okay let's look at our log now we hit a refresh here look at this we had good things a lovely compliment ah nothing to see here we also like smurfs notice that it doesn't look like this it doesn't come out and come back and come out and come back the whole parallel evil timeline is gone and those two evil things that happened separately got squashed picked up and popped on the end of that main that main line so now i'm going to go and say get checkout main so does that parallel world exist notice that main is still sitting here so evil hasn't yet come into maine so let's try this all right and you'll note here all the evil has been rewritten main and evil spock are now the same and if we go back out here we can prove it by looking at the log there was our lovely compliment from our new friend who sent me a pull request and now there's nothing to see here we also like smurfs evil spock has merged itself in by rebasing and rewriting history itself if i then go and push this up into github get my green main again now we can go into met github look at our commits same log as before it's like nothing ever happened now notice here because the branch that evil spock branch that was there for a moment is on my local machine i only pushed main or master up to github from the point of view of that main line it never happened it didn't exist so we've re-based all those changes if we go over here and we look at this you can see that that the branch exists out there but it's it's orphaned it doesn't show up in the main line there's a lot of power in doing that so how do we think about this again remember a merge is when you've created a branch to go and do some work to maybe work on a feature you made a parallel universe when you want to bring those changes back into your main line you usually want to merge that in but rebasing would be when you do a bunch of development and another developer maybe is making an unrelated change you can then pull those changes in take your changes and run them on the top you re-base them you pick a new base not the one you picked a week ago but maybe you'll pick a couple of days ago or today and you'll base your changes on their changes and that's what evil spock did in our little scenario do you need to know rebase it's up to you if you want a clean history if you want a nice linear timeline that has you know not unnecessary merges like you saw me with different friends coming in from parallel universes then you could do a rebase rather than a merge to bring changes in if you don't mind that you can merge them in it really is up to you the thing is you want to avoid rewriting public commits you have to think about are you changing history are you changing only your history are you changing the history for the entire universe the entire project so you want to be thinking about those things it's okay to rewrite some history but not all of it you can always use tools like the tools that i'm using here i'm using again this extension in visual studio code which is called git history that's a nice visual tool that lets you see what's going on and see all the different work and also see the authors and the branches a nice way to see things and i really strongly feel like having a prompt that shows you what branch you're on and what's going on will make your experience learning get a lot easier let me know in the comments what you'd like me to talk about next i think we'll probably look at maybe more detail on squashing and what's called cherry picking also reordering our commits a little bit but i hope this maybe uh demystifies rebasing it's it's not as complicated as you think it's just time travel if you can understand the movie inception and things like that and back to the future you can probably figure out rebasing i wish you all the best in your journey as you learn all the stuff that they didn't teach us in school if you like this and my other videos please tell your friends and subscribe and as the kids say smash that bell thank you
Info
Channel: Scott Hanselman
Views: 41,583
Rating: 4.9744735 out of 5
Keywords: git, tutorial, git rebase, git help, git 101, beginner, codenewbie
Id: hae9zg0-sZY
Channel Id: undefined
Length: 24min 31sec (1471 seconds)
Published: Thu Jul 30 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.