Git without fear · Chapter 12 of 28

Not now, I'm busy

By the end of this chapter you can put half-done work on a shelf, fix something on another branch, and take the work back exactly as it was. 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

You are halfway through the monsoon menu on the monsoon-menu branch: two new lines in menu.txt, a fresh monsoon.txt with notes, nothing committed. Meera walks in: "The app says cutting chai is 250 rupees. Fix it on main. Now, please."

You know how to switch branches, but Git will not do it with your edit in the way:

git switch main
error: Your local changes to the following files would be overwritten by checkout:
	menu.txt
Please commit your changes or stash them before you switch branches.
Aborting

Bunty's idea, "just commit it as wip", leaves a broken half-commit on monsoon-menu forever. Throwing the edit away with git restore loses a morning's work. You need the work out of the way for ten minutes, and then 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

    Fix the cutting chai price on main

    Meera needs a price fix on main now. Your monsoon work is not ready, so put it aside first.

    1. Put the monsoon work aside with git stash (tasks 3 and 4 say how).

    2. Switch to main.

    3. In prices.csv, change cutting-chai,250 to cutting-chai,25.

    4. Stage only prices.csv and commit it.

    To pass: main has a commit of its own that sets cutting-chai,25. That commit changes only prices.csv. None of the monsoon work is in it.

  2. Task 2

    Back to the monsoon work as you left it

    After the fix, go back to the monsoon work, exactly as you left it.

    1. Switch back to monsoon-menu.

    2. Bring the stash back with git stash pop.

    3. Run git status. Do not commit the monsoon work.

    To pass: the price fix is on main, and you are on monsoon-menu. menu.txt is modified (with Ginger chai and the Pakoda plate), and monsoon.txt is untracked. monsoon-menu has only its two commits, "Start the monsoon section" and "Add the menu and prices". No wip commit, no merge.

  3. Task 3

    A named stash, and an empty stash list at the end

    Give your stash a name, so you know what it is later.

    1. Stash the work with git stash push and a message: -m "...".

    2. Run git stash list. Your message is in it.

    3. At the end, bring it back with git stash pop. pop also deletes it from the list.

    To pass: you ran git stash with a message, in the chapter folder, and it worked. A command with a typing mistake does not count, so type it again. git stash list is empty when you press Check.

  4. Task 4 · interview question

    Interview: stash the untracked file too

    Interview question: does git stash take new files? Not by default.

    1. Run git status. monsoon.txt is untracked.

    2. Stash with -u (--include-untracked), in the same command as your message.

    3. Run git status. The folder should be clean, with no monsoon.txt.

    To pass: you ran git stash with -u, in the chapter folder, and it worked. No commit on main changes monsoon.txt.

What you will learn

  • git stash push: saves your uncommitted changes on a shelf and makes the folder clean.
  • -m and -u: a name for the stash, and taking new, untracked files along.
  • git stash list: what is on the shelf.
  • git stash pop: puts the work back and empties the shelf.

More in Undo

Build "Not now, I'm busy" 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.