Git without fear · Chapter 8 of 28
I forgot a file
By the end of this chapter you can add what you forgot to your last commit, instead of making a second "forgot it" commit. 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 the new snacks menu: "Done. Samosas are live." Kabir opens the menu, and its last line says Snacks: see snacks.txt. There is no snacks.txt in the commit, and the snack prices never made it into prices.csv either. Anyone who checks out that commit gets a menu that points at nothing.
With what you know so far, the fix is a second commit, and Bunty is already typing git commit -m "forgot snacks lol". That works, but now the history has a broken commit followed by its patch, forever, and anyone reading it or going back to the first one gets the broken menu. Nobody has this commit yet, so it can simply be fixed.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Put snacks.txt into the snacks commit
Bunty's last commit, "Add the snacks menu", forgot snacks.txt. Put the file into that commit.
1. Run git status. snacks.txt is untracked, so the commit does not have it.
2. Stage snacks.txt by its name. Do not use git add . (it would also stage answer.txt).
3. Add the staged file to the last commit with git commit --amend (task 3).
To pass: the last commit has snacks.txt. It changes only menu.txt, prices.csv and snacks.txt, and no other file.
- Task 2
Put the snack prices into the same commit
The snack prices are in prices.csv, but not in the commit. Put them into the same last commit.
1. Run git status. prices.csv is modified but not staged.
2. Stage prices.csv by its name, together with snacks.txt.
3. Add them to the last commit with git commit --amend.
To pass: prices.csv in the last commit has the snack prices. git status shows no changed or new file, except answer.txt.
- Task 3
Fix the commit itself, with no extra commit
Fix Bunty's commit itself. Do not add a second commit after it.
1. Stage snacks.txt and prices.csv (tasks 1 and 2).
2. Run git commit --amend. Add --no-edit to keep the message.
3. Do not push.
To pass: main has exactly one commit that GitHub (origin) does not have. That commit is "Add the snacks menu", and it has snacks.txt. GitHub's main still has only "Add the menu and prices".
- Task 4 · interview question
Interview: prove that amend makes a new commit
Interview question: does amend change a commit, or make a new one? Show it with a hash.
1. Before you amend, find the hash of the last commit. The short hash is fine.
2. Save only that hash in answer.txt, in the chapter folder.
3. Amend the commit. Then compare: the last commit now has a different hash.
To pass: answer.txt has the hash of "Add the snacks menu" from before the amend, and nothing else. Already amended? The reflog still shows the old hash.
What you will learn
- What a commit left out: reading git status and git show --stat after a commit.
- git commit --amend: rebuild the last commit with what is staged now.
- Amend makes a new commit: a new hash, with the old commit still listed in git reflog.
- When not to amend: once a commit is pushed.
More in Undo
Build "I forgot a file" 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.