Squash Commits Before they are Pushed with Interactive Rebase

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

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.

Instructor: [00:00] 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.

[00:13] 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.

[00:35] 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.

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

[01:01] 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 <esc> :wq to save that.

[01:21] 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.

egghead
egghead
~ 2 hours ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today