Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    How to squash multiple git commits


    Often, project maintainers prefer that a single pull request be represented by a single commit. It makes the git history cleaner and easier to understand. So before your pull request is merged you’ll want to do an interactive git rebase to squash all of your commits and fix your commit message.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Now that our changes have been reviewed and the maintainer is good to bring them in, let's clean up our pull request a little bit by squashing both our commits into a single commit and updating the commit message to follow the standards.

    Let's start at the interactive git rebase. To start a rebase, we need to provide the parent commit of the oldest commit for the start of the rebase. If we enter "git log," we can see that the oldest commit we care about is our first commit here. Let's copy the SHA of the parent commit and use that to do our rebase.

    Now we'll run the command "git rebase -i." This stands for interactive, which will give us the opportunity to alter individual commits in the process of our rebase. Then we'll paste in the SHA that we just copied.

    That will open our default editor with the rebase file. I should note that, for me, this is opening in VIN. If you're not comfortable with VIN, look up how you can change the default editor for Git so you can use an editor you're comfortable with.

    There are a few things you can do for each commit in an interactive rebase. Those are listed in the comments section in this rebase file. I won't discuss all of these in this lesson, but it's helpful to note that an interactive rebase with Git is incredibly powerful.

    You can think of each line as a command for the rebase to run. If we remove a line, that command is removed and the commit is skipped during the rebase. The commands run, from top to bottom. For us, we simply want to combine these two commits into a single commit and change the commit message for that single commit.

    To do this, we're going to use the squash command for the bottom commit, which will meld it into the previous commit. If you had more commits you wanted to squash together, you would use this same command for each of them.

    With this order of commands, it's first going to apply this first commit and then it will squash this second commit into that first commit.

    Now, we'll save the file and close our editor. Git will begin to execute our commands. Our editor has opened again because the squash command requires a new message. Here, it's giving us the message for both commits being squashed.

    We'll clear this out and save the file with the message we want for the squashed commit.

    That's it. If we run git log, we can see that we now have a single commit with a proper commit message. We're good to push our changes to update our pull request.

    Remember, a rebase is rewriting history so we need to force push to our branch to update our pull request. Otherwise, our push will be rejected. Also remember that this is a destructive operation. You want to double check that you know what you're doing before you do this.

    Now we'll enter "git push -f" to update the PR. Now we can look at the pull request and it looks terrific. There's a single commit with a conventional commit message. The git dif is exactly right and all the checks have passed. This is ready to be merged.

    It's important to note that pushing updates to a pull request will not notify the project maintainer or anyone else watching the pull request, so if you want the maintainer to receive a notification you'll need to leave a comment indicating that you've updated the PR and are waiting patiently for feedback. We'll do that now.