Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

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

Autoplay

    Squash Commits Before they are Pushed with Interactive Rebase

    Chris AchardChris Achard
    gitgit

    We have created 3 commits that we want to squash together before we push them to github. We can enter an interactive rebase with:

    git rebase -i HEAD~3

    and then change pick to squash for the last two commits (we want to squash "down" into the first one).

    Then we'll be given the chance to make the commit message for that commit, and once we save the message we'll be left with just a single commit that contains the changes from all three commits.

    Code

    Code

    Become a Member to view code

    You must be a 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
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: Let's take a look at the git log oneline. We can see the three commits that we haven't pushed yet. Let's say, instead of having all this history, we just want to push this as one commit to the origin.

    We can do that with interactive rebase and we can do a git rebase -i, for interactive, and will let you go back three to handle all three of these. We're going to do HEAD~3 and we want to squash this all together. Our command here is going to be squash or we could also use fixup, but squash lets us re-write the commit message.

    The idea here is we have to squash new changes down into another change. Let's enter I for insert mode and instead of pick here, we're going to squash this down into Change 2. We're on the squash Change 2 down into Change 1.

    Notice that these are assorted, so that the top one is the oldest one. We can hit :wq to save it. Now we are in a new squash merge message.

    We could leave all this the same if we wanted all of the different messages or we could enter insert mode and delete all of the old messages. We could just say, "This is our new alert change" and hit :wq to save that.

    If we do a git status, we have just one commit. If we do git log oneline, we have all three of our commits squashed into one commit that we can then push. If we do a git push now, then all three of our changes have been pushed in one single commit.