Check out a free preview of the full Everything You'll Need to Know About Git course

The "Interactive Rebase" Lesson is part of the full, Everything You'll Need to Know About Git course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates how to use interactive rebase to squash multiple commits into one. He also shares their personal workflow for squashing commits and pushing changes to a remote repository.

Preview
Close

Transcript from the "Interactive Rebase" Lesson

[00:00:00]
>> I know this may be shocking. There's actually more with Rebase. We can continue to rebase forever. So this one won't be as long of a part, it's not nearly as confusing, but we're still rebasing, okay? You can do a lot with Rebase, but I really have only ever used it to squash commits.

[00:00:19]
I don't use it to edit commit messages or to drop commits. I don't think I've ever dropped a commit in my lifetime. It's an option, you just put D in there and it just drops it, but I've never actually done it. So there's a lot of options, you just don't have to use them all.

[00:00:33]
I always use it for squashing everything. And so let's set up the repo with three just quick sample commits in the remote-git at the end of the README.md, and just say, added one to the end of the line, or something along that line. Whatever you wanna or however you wanna do, just three quick, one after another at the end.

[00:00:49]
So I'm just gonna go echo "1" to the end of README, git add this, git commit "1" to the end, and then do it with "2". I can find myself wanting to write a little bash script to do it for me, but it's so small, but at the same time it just feels so right to automate this, 3.

[00:01:11]
Do you guys ever have some sort of automation paralysis instead of doing the thing? That was three small commits, I'm like, I gotta write a bash script for this. I can't be spending time writing the same thing three times, it's ridiculous. Automation paralysis, real thing. All right, so there we go.

[00:01:27]
So if I go git log --oneline 4, you can see, one, two, three, then our a plus 10. Okay, cool. We have these three new commits here. Now what we're gonna do is we're gonna use Rebase Interactive. So how Rebase Interactive works, is that you do a git rebase, you do an i, and then you add a commit-ish.

[00:01:45]
A commit-ish is a commit or something like a commit. So head tilde some number, starts at your head and walks back a commit, however big that number is. So if I go head tilde 1, that's one commit back from head. If I do head tilde 3, that's three commits back from head, so-

[00:02:05]
>> Not being glib, this is a genuine question. Commit-ish is that a production term? Is that a term that you'll find in the man pages?
>> I'm pretty sure, I'm not making this up. I'm pretty sure, what are commit-ish and tri-ish in Git? There you go. So I thought, it's a good thing to know, a commit-ish.

[00:02:24]
There's a dash in it. I didn't put a dash in there. But it's a fair question. I do say things in a rather abstract way sometimes. This is just not one of them. All right, I should have put a little dash right there, though. All right, so let's do an interactive rebase with these three commits.

[00:02:42]
And let's use the head. By the way, it does make sense. It's an odd word. It does make sense, though, and I like it. All right, so we're gonna squash three commits. So do git rebase -i, and then head tilde 3. You wanna squash three commits. So git rebase -i HEAD~3.

[00:03:02]
And this makes sense because what you're doing, is that you're going back three commits, which is actually not, it's the one commit before these three, is three commits back. We're gonna go there and then it's gonna replay these three commits on top, but we get the chance to say, how do you replay it?

[00:03:20]
So this makes perfect sense with how Rebase works. We're just now able to say what to do. And it comes with a really nice little comment right here to kind of tell you what you should do. So as you can see right here, squash. As for squash, and the commit will be meld into the previous.

[00:03:37]
So if I go right here and go s, 2 and 1 become one commit, and so get melded into the previous. If I do an s right here, it'll be now 2 and 3, and 1 and 2 all become one commit. And so this is the interactive side.

[00:03:54]
You get to choose how it replays. And so I'm gonna choose these things and then I'm gonna save. When it does that, it gives you the option. It puts all the commit messages into a single file for you, and gives you the option to change it however you wish.

[00:04:08]
So I'm just gonna go here, do that. And I can say 1 through 3 to the end. Why not? There we go. So now if I go git log, I'll do the one line form. You'll see right here, 1, 2, 3 to the end, I've taken all three commits and turned it into one commit, okay?

[00:04:26]
So this is that whole history editing thing people talk about. And if you do this and you push it to a remote, and then you do it again and try to push, it's gonna say no, you can't do that, you've just changed histories. That's when you use force to say, no, I know best.

[00:04:42]
I make history and some rage against the machine who controls the past now and then boom, you're off and running. My general workflow, by the way, I have already talked about it. Do squash me messages, bunch of little small commits, turn it into one commit, push it up there.

[00:04:55]
And then inevitably like every other human on earth, I push up my commit, I add the people that are gonna review my commit, then I look at my commit on stash, and go, man, that was such a stupid commit, I can't believe I just put people on this.

[00:05:07]
And then I have to go through really quickly and try to make some changes, but then I wanna still be one commit, and then I'm like squashing a commit, amending, and force pushing. It's a very embarrassing cycle one has to go through.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now