How To Squash Your Git History Like A Pro

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone it's colt i'm here to talk about git and specifically the joys of squashing if you're interested in learning more about git definitely check out my other videos uh or my recently released skid course which has nothing to do with me releasing a get youtube video just a coincidence anyway check it out if you're curious there's a coupon below in the description i'm pretty happy with how this course came out really i think it's my best course ever not just saying that all right so let's learn how to squash yes this is funny you can laugh also this is squash that's even funnier and you should be laughing more anyway what we're actually talking about within gitz is this idea of squashing commits together combining multiple commits down into a single commit so this is kind of a stylistic choice or a workflow decision it's not something you ever have to do but here's how it works if you have typically a feature branch that you're working on it could be messy it could have disastrously ugly commit messages and just way too many commits if you want to combine them you can you could squash them down into a single commits that you could then merge into master or you could rebase on top of master there are different workflows that involve squashing but the idea is taking multiple commits and combining them boiling them down into a single commits so why do this well to keep your history tidier and easier to read you'll retain all the changes you're not losing work but you are losing commits but deliberately uh you'll get rid of messy feature branch commits and you won't muddy the main line history as much but it's just a personal choice or the company that you work for the group that you're in somebody makes a decision that we want you to squash before you merge changes so you never need to do it but here's an example this is not an image i created it's from a blog post on carbon 5 that shows a very messy get history and compare that to this image here of a large repository that is using a squash and rebase based workflow where branches feature branches are squashed down to single commits and then rebased a very obvious difference in readability and headache inducingness another reason you may want to squash is that if like me you were told to commit early and often commit all the time you might end up with some really stupid commits or commits that you don't want to be part of the the public record you don't want to share with other people not because they're like horribly embarrassing but they're just frivolous or you know small really trivial things that could be combined or boiled down squashed into other commits so you might have history that looks like this you know fix some issue work in progress it's the weekend i've just got to make a commit now because it's the weekend i'm not doing anything else tweak something fix a typo uh there's so many of these little things that might come up we could squash them down the repo that i'll be using in this short video is a github repo i've made it's nothing fancy at all there's just two branches there is the main branch and then there is a about me branch if you want go ahead and clone it the link is in the description let me just run git clone with that url and then we'll check out this new folder so there's two files in here and if i do a git log on the main branch we've got some basic commits for different features different sections of a website so add the hero section add a resume section at a contact section and i'm just going to open this up what is it index so this is from a template i did not make this myself this is from a bulma a css framework one of the templates that i found on github uh so very nice i didn't create that i want to be very clear about that anyway so there's a new section i'm working on called about me so i have a spot for it but there's nothing in there so i have a branch where i've been working on it and that branch currently is just a remote branch if we do a dash a there it is it's on github i don't have it but if i do a git switch and i just use that name git will make me that branch locally and it's going to set it up to track the remote branch and anyway i now have that branch here and if i do a git log one line i added all these commits on this about me branch they all have to do with making the new about me section so i started it i added some new markup i tweaked the padding and the fonts then i made it responsive and i updated the copy in the the index.html file i fixed a typo um it's kind of it's a little messy it's it's not great if we take a look it doesn't really matter but there's that new section um i want to squash this all down all of these commits right here into a single new commit that i could then incorporate into main um so to do that we need to talk about interactive rebasing so you don't have to be an expert on rebasing uh but there's this option we can pass into the git rebase command called dash i well it's not called dash i the dash i option which tells git that we want to enter the interactive rebase mode which allows us to do a whole bunch of things we can squash commits we can change commit message commit messages we can add files or edit the contents of commits we can drop commits there's quite a few things we can do but before we talk about that we have to tell git how far back we want to rebase so in this example here if i want to squash all of all of these together i need to tell get i want to go back one two three four five six commits or i can just pick a commit hash like this this is where we diverged i could rebase back to this point and you don't have to be an expert on rebasing i want to be very clear about that um i do have that video on it if you want to check it out but if i just copy this commit hash and then i run git rebase dash i and i put that hash in it's going to open up my text editor and in my editor i'm going to see a list of the commits that i'm set to rebase and then it's going to say pick in front of each one and i can change that command there's a series of commands reword edit squash that's when we wants drop and if i change those and then i close out of the file git will go through this file and for each one of these commits it will run a particular command whatever we specify so i could say drop this commit or edit this command or reword this commit and git will listen it will do those that will execute those commands so for us we're focusing on squashing so the way that this squash command works is that wherever we replace pick with squash all of those commits will be combined together so all of these will be combined into the most recent commit that's not being squashed which would be this one here so they all get squashed up into this one f7f add new carousel feature so once we save this file we close it git will execute that and then you'll see your editor opens back up and it has a new commit message that you can then go and edit so let me demonstrate this i'm on the about me branch and i already copied this hash i'm going to go back right to here so i want to rebase all of this stuff everything on the about me branch so i'm going to run git rebase dash i and paste that hash and it opens up my editor and you can see that we have all those commits so we're not rebasing the entire history right we specified a point we want to go back to and they all say pick to start pick just means use this commit in the rebase but we don't want that we want to squash things so i'm going to squash this one and this one and this one and this one and this one i don't have to do that i could just squash this one and it would be combined with the commit above it or i could just squash these two and combine them with this one up here it's up to me but i'm going to squash all of them except the first one so i'm just going to select them all and i can write squash or i can just type s i like to do squash personally just i don't know it's more explicit and then i close the file so i'm going to x out of it and then it opens up my editor again because now there's a new commit message the commit message it generates for me looks like this says this is a combination of x in my case six commits here's the first one the second one the third the fourth or fifth i can delete all of that if i don't want that i can get rid of it and replace it with my own message i'll just leave it there right we still have a record of those commit messages this is the last record of them because those commits are gone the actual commit objects aren't part of the history anymore right we just have one commit right here and this commit contains all of the changes that we made that we squashed together so if i just do a regular git log you can see the long commit message and it contains all that stuff right nothing has changed if i refresh this page we still have the whole feature here that used to be made up or it used to be spread across six commits now it's just one commit so at this point i could merge that in this new commit into master i can rebase i can do whatever i want but the focus of this video is just on the actual squashing so we accomplished that that was one option for squash and commits there is one other option which is to use the dash dash squash option when we use git merge now this behaves a bit differently you have less control in this scenario so with this interactive rebase option i could selectively squash you know this one commit into the prior commit um i don't have to squash everything on the branch but if we use this option we'll merge a feature branch typically into some other branch with dash dash squash that tells git to take all the changes no matter how many commits just all the changes from the feature branch apply them to the current branch and then it's up to us to actually commit that we have to make the new commit so i'm going to undo what i just did on the the branch here uh using a reflog reference that's something i cover in the git course as well i'll probably make a video on it one day um anyway if i do a git log i've now undone uh the squash that i had and now what i'm going to do is just demonstrate this other way of squashing changes i'm going to switch to my main branch all right and i'm going to now merge using this squash option the about me branch so normally if i just did a git merge about me i'm gonna have all of those commits whatever number will be in my history six commits or so from this branch uh but now i don't have a new commit just yet if i do a git log i don't have a new commit the last one is still add footer which is what it was before but if i type git status i have changes that are currently that get made for me that i have to commit myself and if i take a look at them i'll just do git diff head we can see this is all the stuff coming from that branch so all this work is here all those commits all the changes from those commits was added over to my main branch and now i have to make the commit myself so they're already staged and i'll just do something like um you know add about me section and now if i do a git log i'll just do one line i have a single new commit add about me section on the main branch and it contains the six or so commits from the about me branch smushed together so all that work is here if i refresh the page here's the about me section totally functional all the work is still there but now we just have one commit so this is just a choice that you can make if you want to squash things you can i'm not saying you need to all the time some people are not a fan because you do lose a history right you're rewriting history you're combining things together but on the other hand you do end up with a much cleaner history this is what we see right now on the main branch if i had not squashed this is what we would have seen on the main branch all these additional tiny commits they add up to be a feature but on their own they're kind of some of them at least are silly so we saw two ways of squashing one that we just saw is to use the squash option with merge and the other is to use interactive rebase which gives us finer control over what we are squashing we don't have to do all commits like i did here you could single one out and squash it into its neighboring commits either way the idea is to clean up your history just squash commits together so i hope you enjoyed that or at least found it informative if you want to learn more about git check out my other git videos or my recently released git course that goes into a ton more detail lots of fancy diagrams pretty happy with how it turned out you can find a coupon in the description alright goodbye everyone
Info
Channel: Colt Steele
Views: 9,372
Rating: 4.9818592 out of 5
Keywords:
Id: RwvTrSm7zEY
Channel Id: undefined
Length: 13min 9sec (789 seconds)
Published: Thu Mar 18 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.