Skip to content
← System engineer foundations

Learning bite

Undo, recovery, and repository hygiene

Choose a recovery operation according to where the change exists.

Documentation reviewed2026-10-01 · 3 min read
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.

SituationOperation to considerWhat remains
Staged the wrong pathgit restore --staged PATHWorking file still contains the edit
Want to discard an unstaged fixture editgit restore PATHWorking path is replaced from the index by default
Need to reverse a shared commitgit revert COMMITOriginal commit plus a new corrective commit
Lost a recent local branch positiongit reflogLocal 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:

bash
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:

bash
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:

bash
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.

Sources

Primary references: Git restore↗; Git revert↗; Git reflog↗.

Your notes and evidence

Record observations, questions, or links to your work. Keep credentials out of your notes.

Loading saved progress…

Back up or restore this path

Progress and notes stay in this browser. A backup contains only this learning path.