Git without fear · Chapter 9 of 28

Undo the last commit

By the end of this chapter you can take back a commit you have not pushed, keep its changes, and commit them again properly. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Undo
  • 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 commits and turns his screen around, proud. One commit called "updates" holds two unrelated things: the bun maska price going down to 50, which Meera asked for this morning, and combos.txt, a first go at chai combos that Meera has not even seen. If she says no to the combos, there is no way to drop them without dropping the price too.

Amend from the last chapter only adds to a commit; it cannot split one. Committing again on top leaves "updates" in the history for good, and undoing it by hand means editing both files back and remembering the old price. The commit is not pushed, so it can simply be taken back.

What you build: 4 tasks

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

  1. Task 1

    Undo the "updates" commit, but keep its changes

    Bunty's "updates" commit mixes two changes. Take the commit off main, but keep its changes in your files.

    1. Run git status. main is 1 commit ahead of origin/main, so the commit is not pushed.

    2. Use git reset to move main back one commit.

    3. Pick a reset mode that keeps the changes. Do not use --hard.

    4. Do not push.

    To pass: no commit on main is called updates. main still starts from "Add the menu and prices". combos.txt still has the combos, and prices.csv in the chapter folder still has bun-maska,50.

  2. Task 2

    Commit the bun maska price fix on its own

    Commit the price fix alone, in its own commit on main.

    1. Run git status. See which files are staged.

    2. If combos.txt is staged, unstage it. It stays in the folder.

    3. Stage only prices.csv, and commit it with a clear message.

    4. Do not push.

    To pass: a new commit on main changes only prices.csv, with bun-maska,50. Its message is not updates.

  3. Task 3

    Commit the combos on their own

    Commit the combos in a second new commit on main.

    1. Stage only combos.txt.

    2. Commit it with a clear message about the combos.

    3. Run git status and git log --oneline to see the result. Do not push.

    To pass: a new commit on main adds only combos.txt, and its message is not updates. main has exactly two commits that GitHub does not have: the price fix and the combos. git status shows nothing left, except answer.txt.

  4. Task 4 · interview question

    Interview: the reset that throws changes away

    Interview question: git reset has three modes, soft, mixed and hard. Which one also deletes the changes in your files?

    1. Think about what each mode changes: the branch, the staging area, your files.

    2. Save the name of that mode in answer.txt, in the chapter folder.

    To pass: answer.txt has only the name of that mode. You may write it with or without the two dashes.

What you will learn

  • git reset: move the current branch back to an earlier commit.
  • HEAD~1: the commit one before HEAD.
  • Soft, mixed and hard: what each mode does with the changes, and which one throws them away.
  • One idea per commit: committing the two changes separately.

More in Undo

Build "Undo the last commit" 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.