Git without fear · Chapter 18 of 28

Squash the WIP

By the end of this chapter you can fold a branch's messy commits into one clean commit with a real message, without changing the code. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Branches
  • 4 tasks
  • Git
  • In the all labs plan

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.

Why you need this

Bunty's bill total works, and he is proud of it. Kabir opens the branch:

git log --oneline main..bill-total
8a6e7d0 final final
d767d37 fix
814f9cf wip 2
64c26f6 wip

main..bill-total means "commits on bill-total that main does not have". Four commits, one idea. Before this goes into main it must become one commit with a name a human can read in a year. Bunty: "final final was very descriptive, no?"

The Git you know can only add commits on top. Amending fixes just the last one, and a merge would carry all four into main, "wip" and all. Squashing rewrites them as one commit with the same final code. You are on bill-total, and nobody else has these commits, so rewriting them is fine.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Squash Bunty's four commits into one

    Turn Bunty's four messy commits on bill-total into one commit. Nobody else has bill-total, so you may rewrite its history.

    1. Run git log --oneline main..bill-total. It lists Bunty's four commits. Note the hash of final final.

    2. Stay on bill-total. Do not change main.

    3. Squash the four commits into one. Use a soft reset to main, or an interactive rebase, as in the lesson.

    4. Make one commit with all the changes.

    To pass: bill-total has exactly one commit that main does not have, and it comes right after main's last commit, "Add the price list". main keeps its two commits, Add the menu and Add the price list.

  2. Task 2

    Keep the code exactly as in final final

    Squashing changes the history, not the code.

    1. Do not use git reset --hard. It deletes the changes.

    2. After the squash, compare your commit with final final's hash. git diff with that hash shows no difference.

    To pass: the one commit has menu.txt, prices.csv and bill.js, and no other files (answer.txt is allowed). bill.js is exactly as in final final.

  3. Task 3

    Give the commit a real message

    Give the one commit a message that says what it does.

    1. Write a subject (the first line) of at least three words.

    2. Do not use the words wip, fix or final anywhere in the message.

    3. A squash keeps the old messages under the first line. Delete them all.

    4. To change the message later, use git commit --amend.

    To pass: the subject has at least three words, and wip, fix and final are nowhere in the whole message, not even under the first line.

  4. Task 4 · interview question

    Interview: fixup or squash?

    Interview question: how is fixup different from squash in an interactive rebase?

    1. Read the help in the to-do list of an interactive rebase. It explains fixup too.

    2. Write one line: with fixup, what happens to the message of the commit you fold in?

    3. Use the word fixup in it. Save it in answer.txt, in the chapter folder.

    To pass: answer.txt has the word fixup and says what happens to that commit's message.

What you will learn

  • reset --soft: moves the branch back and keeps every change staged, ready for one commit.
  • rebase -i: a to-do list of commits that you edit, to squash them.
  • squash and fixup: both fold a commit into the one before; fixup also drops its message.
  • A message worth reading: what the commit does, not how the day went.

More in Branches

Build "Squash the WIP" yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Git without fear are free. This one comes with the all labs plan.