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
it out. Before we get started you'll want to make
sure that you have the GitLens
Supercharged extension installed, and once that's done from the
command palette, because we're going to be demonstrating rebasing, 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: 3 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 GitLens
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 rebase 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 git rebase.
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 to 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 ref heads
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 EU arrow key
to bring U the last command that I wrote and
justice going to modify this to say git rebase.
New feature now by default it will open this U
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 and that it
allows you to rewrite your commit history. Now what's squash
specifically does is that it squashes 2 or more commits.
Into a single commit, squash them retroactively. Make that happen.
Now, this can be really useful for keeping your history
streamlined. O 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 A 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
Git 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, fixup 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 remove brother.
Select squash there.
And also change friend to brother and then click start
rebase.
And now a window opens U and here what we
can do is comment out the messages that we want
to ignore.
O that's align 8 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 control 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 could see it said successfully. Rebased and updated refs
slash heads new feature.
And at the top here we could see that we've
squashed our 3 commits into one, which now says our
original message finished a 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- I new feature. So once again when this
view with this editor shows U, 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 3 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.