Learning bite
Undo, recovery, and repository hygiene
Choose a recovery operation according to where the change exists.
Git
Predict before you try
What would differ if the wrong line had never been committed?
Try it: Locate the change before choosing an undo →On this page
Locate the change before choosing an undo
“Undo” can mean keep an edit but unstage it, discard an uncommitted edit, reverse a commit, or recover a commit whose branch name was lost. Those are different operations. Inside the disposable repository, begin with git status, git diff, and git log --oneline -5. Preserve valuable uncommitted content before discarding anything.
| Situation | Operation to consider | What remains |
|---|---|---|
| Staged the wrong path | git restore --staged PATH | Working file still contains the edit |
| Want to discard an unstaged fixture edit | git restore PATH | Working path is replaced from the index by default |
| Need to reverse a shared commit | git revert COMMIT | Original commit plus a new corrective commit |
| Lost a recent local branch position | git reflog | Local record may identify a commit to name again |
These are alternatives, not a command sequence for an unfamiliar repository. A restore can discard changes that Git never committed. Reflog records movements of references, not a complete history of everything typed in an editor.
Unstage without losing the text
Start with clean status, then make one clearly temporary change:
printf '\nTemporary draft: check this wording.\n' >> runbook.md
git add runbook.md
git diff --cached
git restore --staged runbook.md
git status --short
git diff
Expect M runbook.md: a working edit remains, but it is no longer staged. Read the diff to confirm it is only this disposable draft, then discard it:
git restore runbook.md
git status --short
Status should be clean. This demonstrates why “unstage” and “discard” cannot be used interchangeably.
Reverse a recorded mistake
Now make an intentionally wrong instruction, label it clearly, and capture its actual commit ID:
printf '\nDELIBERATELY WRONG: enable always starts a service immediately.\n' >> runbook.md
git add runbook.md
git commit -m 'Add deliberate incorrect instruction for recovery practice'
study_bad_commit=$(git rev-parse HEAD)
git show "$study_bad_commit"
git revert --no-edit "$study_bad_commit"
git log --oneline -3
git diff HEAD~1 HEAD
The last diff should remove that wrong line. The history retains both the mistake and its reversal. The technical correction matters: systemctl enable alone configures activation relationships; start or enable --now also starts the unit. If later edits overlap a revert, Git may ask you to resolve a conflict; inspect it, or use git revert --abort to leave the in-progress operation.
Recover a name before changing content
Run git reflog -5 to see recent local reference movements. It can help find a commit after an accidental branch move or detached-HEAD work. Once you identify the wanted commit, creating a new branch at it gives it a stable name without replacing the current working files. For example, git branch recovery/notes ACTUAL_COMMIT is a pattern to use only after replacing the placeholder with a verified ID.
Reflogs expire and are local, so they are not backups. Never-committed content may be unrecoverable. A stash can temporarily store selected local changes, but it is also local and may conflict when reapplied. Neither is a reason to skip clear commits or backups.
reset can move a branch and, depending on its mode, change the index and working files. You do not need reset --hard for these exercises. A force push changes history seen by other users; even --force-with-lease requires care and coordination. A shared bad instruction is normally easier to explain with a new corrective commit.
What would differ if the wrong line had never been committed? A scoped restore could discard it after review; a revert needs a commit to reverse. Keep the repository for the recovery lab, or remove it through your file manager only after confirming its path and contents are all disposable practice work.
Apply this to MicroBank
Revert a deliberately incorrect statement in the disposable MicroBank runbook and inspect the resulting history. Then repeat the relevant local fixture check. Git recovery proves which source text is present; it does not revert database writes, running containers, or infrastructure state. Those operations need their own recovery procedures in later paths.
Keep this observation in the MicroBank readiness project.
Sources
Primary references: Git restore↗; Git revert↗; Git reflog↗.
Prove what you learned
Inspect the result
Retain status/diff evidence, the relevant commit identity and the recovery result.
Explain it in your own words
Explain why your observation supports the stated outcome, and name one thing it does not prove.
Try a small variation
Make a second uncommitted edit after staging and predict which version a commit would record.
Your evidence should show
- Record the context/revision or label the work as a paper or prepared fixture.
- Compare the prediction with the actual result from “Locate the change before choosing an undo”.
- Retain status/diff evidence, the relevant commit identity and the recovery result.
- Explain the transfer challenge and retain the same verification question.
Hint
Compare working tree, index and committed snapshot.
Answer guide: compare after predicting
A scoped restore could discard it after review; a revert needs a commit to reverse. Keep the repository for the recovery lab, or remove it through your file manager only after confirming its path and contents are all disposable practice work.
Compare this with your actual observation. A paper answer is useful reasoning practice; record execution separately.
Recall why a runbook change needs the same behaviour check after recovery. Revisit the previous lesson.
Save the prediction, result and explanation ↓Your notes and evidence
Record observations, questions, or links to your work. Keep credentials out of your notes.
Back up or restore this path
Progress and notes stay in this browser. A backup contains only this learning path.