Git Rebase & Squash in VS Code using GitLens Supercharge

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone so in the series of git related videos that I've been publishing many of you have asked for one on the rebase and squash commands so in this video I want to explain what both of those are and how to perform them in vs code with an extension called git lens so let's check it out before we get started you'll want to make sure that you have the git lens get supercharge extension installed and once that's done from the command palette because we're going to be demonstrating rebasting make sure that you enable interactive rebase editor as you see here at the very top once both of these are done we can get started when we talk about rebasing especially in the context of commits three things to keep in mind one we're referring to taking commits from a branch and replaying them at the end of another Branch two this comes in handy to integrate recent commits without merging and three this also results in a cleaner more linear project history what rebase does is it takes the commits in our new feature branch and it shifts them down to the tip of the master Branch now one more thing to note that's very important is that you never want to rebase a public Branch it's just too risky it's generally for private branches that you use exclusively now for a deep dive into rebasing check out the official documentations why don't we go ahead now and try rebasing some code using our git lens extension now back in Visual Studio code I'm going to go ahead and click on the source control icon now I've already set up my main branch on GitHub with my code which is just a simple file that prints rise and shine but for now I'm going to go ahead and create a new separate branch called new feature so I'll click on create branch in my control palette spell out a new feature on this Branch I'm going to make changes to my app by adding some code so that I can rebase those changes onto the main branch so for example I'll add a line here console log ready for a new day now we're going to go ahead and commit that I'll add a message that says added a line when I click commit I'm going to want to move on and add one more line let me just copy and paste this and we'll change this to today's the day and we'll go ahead and add a message that says added another line and we'll commit that and by the way if you happen to notice that I didn't stage manually that's because you can configure Visual Studio code to automatically stage for you so that's why it didn't prompt me to Stage so I've made and committed two changes to my new feature Branch so now it's time to rebase them onto our main branch and to do that we just need to go down and select commits here and right on this icon here clicking it will show our commit graph and this essentially shows you a visualization of your commit history you can see our commits from the main branch that was originally here along with the commits that we just added to the new feature Branch now the next thing we want to do is to open up our interactive rebase editor which will allow us to rebaste these changes onto our main branch but in order to do so you want to make sure that you have Visual Studio configured to launch that editor and you can do that by running this command here now I'll clear my console and the next thing that we want to do is to run a rebase command from the terminal by typing get rebase hyphen I for interactive and our Target branch which is Main now our editor is open and the editor's interface is going to reference our commits the commit messages and when the commit was made now there's also the option here to disable the editor switch the text abort the rebase on the far right but we just want to start the rebase so we'll select that and as you can see the rebase was successful in a terminal it says successfully rebased and updated refs heads slash a new feature now this is good but it hasn't updated the main branch yet so let's go ahead and switch back to main and this time I'm going to hit the up Arrow key to bring up the last command that I wrote and just going to modify this to say get rebase hyphen I new feature Now by default it will open this up but we don't need this now and what we want to do is go to our commits graph and as you can see now it has all of our commits in our main branches commit history and it looks a lot cleaner so let's turn our attention now to the squash command squash is similar to rebase in that it allows you to rewrite your commit history now what squash specifically does is that it squashes two or more commits into a single commit squash can retroactively make that happen now this can be really useful for keeping your history streamlined so let's go ahead and take a look at an example and to do that let's hop on over to our new feature Branch from the command palette I'll choose new feature and now let's start with making some changes that we can commit and ultimately squash let's say I added a line console log goodbye friend and I felt that that was all I wanted to do so I put down finished app and committed that but then I gave it some thought and I'm like nah a friend just sounds a little too generic how about if I change it to goodbye brother and I went over here and added a message saying change friend to brother and committed that but then I thought about it again I'm like no well what if it was a sister that I wanted to put down and maybe I shouldn't specify either sibling and I should just change it to goodbye now I'll come here and add a message and say that I removed brother and committed that and now I'm finally just happy with the app but even though I'm happy with the app let's say I wanted to hop over to look at our commit graph and what I see is that I have three different commits for just one line of code which is making it overly complicated so what we want to do is squash a couple of these into one commit and we could do this with the interactive rebase editor so from the command line I'll type get rebase Dash I like we've done before Main and once the editor opens we can see the changes that we've made the messages that we've written and a drop down menu which is defaulting to pick but you could also choose reword edit squash fix up and drop and what we're going to want to do is choose squash for the commits that we want to squash so we'll want to do it for removed brother select squash there and also change friend to brother and then click Start rebase and now a window opens up and here what we can do is comment out the messages that we want to ignore so that's line eight change front to brother and I'll just add a pound in front of that to comment that out and I'll do the same for line 12 remove brother so now I'll make sure that I save this and hit Ctrl s if needed but my visual studio code is configured to save automatically so I'll just go ahead and close this and in our terminal now you can see it said successfully rebase and updated refs heads Slash new feature and at the top here we could see that we've squashed our three commits into one which now says our original message finished app now if we want to update our main branch all we need to do is switch like we did before from the command palette I'll go ahead and switch to Main and from the terminal I'll type git rebase hyphen I new feature so once again when this view with this editor shows up we could just go ahead and close that and now what we see is that our main branch has updated and rebased as well and when you look at our commit graph all these changes are reflected in our main branch including our finished app commit which was originally three commits that has been squashed into one and that's about it now rebasing does take some getting used to so with practice you'll learn to appreciate it a lot more but for now let me know in the comments your thoughts or any other source control topics that you want to cover in the future if you got value out of this video don't forget to hit that like And subscribe button and check out some of these other videos here I'll see you guys in the next one
Info
Channel: Visual Studio Code
Views: 26,983
Rating: undefined out of 5
Keywords: vscode, visual studio code, vs code
Id: 3o_01F04bZ4
Channel Id: undefined
Length: 9min 57sec (597 seconds)
Published: Mon Nov 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.