Git without fear · Chapter 7 of 28

Undo before you commit

By the end of this chapter you can throw away the changes in one file and take a file out of the staging area, without touching anything else. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Undo
  • 3 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 had an afternoon. He tried to reformat menu.txt and it came out as asdfgh. He typed Meera's draft prices into prices.csv and staged them, though she has not approved them. And in between he wrote a good withTip function in orders.js. Kabir needs the menu fixed, the draft out of the next commit but kept for Meera, and the tip function left alone.

"I'll just git reset --hard," says Bunty. That puts every file back to the last commit at once: the mess goes, and so do the tip function and the draft prices, which were never committed, so Git has no copy of them. Editing menu.txt back by hand means remembering four lines exactly. You need an undo for one file at a time.

What you build: 3 tasks

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

  1. Task 1

    Undo the mess in menu.txt, and only there

    Bunty broke menu.txt. Undo his changes in that one file, and keep his good work in orders.js.

    1. Run git status and git diff to see what changed.

    2. Undo the changes in menu.txt with git restore. Name the file.

    3. Do not restore orders.js, and do not commit.

    To pass: menu.txt is exactly as in the last commit, and not staged. orders.js still has Bunty's withTip function. You made no new commit.

  2. Task 2

    Unstage the draft prices, but keep them in the file

    Bunty staged draft prices that Meera has not approved. Take them out of the staging area, but keep them in the file.

    1. Run git status. prices.csv is under Changes to be committed.

    2. Unstage prices.csv with git restore and the --staged option.

    3. Run git status again. prices.csv is now under Changes not staged.

    To pass: prices.csv is not staged, and the file still has Masala chai,45. Do not restore the file itself after you unstage it. You made no new commit.

  3. Task 3 · interview question

    Interview: the older command for restore

    Interview question: before git restore, which git checkout command did the same as git restore menu.txt?

    1. The older command is git checkout, with a double dash before the file name.

    2. Write the whole command, with menu.txt, into answer.txt in the chapter folder. Put it in quotes, so the shell does not run it.

    To pass: answer.txt has the git checkout command that undoes the changes in menu.txt.

What you will learn

  • Reading git status: which files are staged, which are only changed, and the hints Git prints for each.
  • git restore: throw away the changes in one file.
  • git restore --staged: unstage a file and keep its changes.
  • The older spellings: git checkout -- and git reset, which you will still meet in answers online and in interviews.

More in Undo

Build "Undo before you 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.