B. Git task and recovery reference
Begin a recovery by naming the repository, current branch, working changes, staged selection, and recent history. Decide which content must remain recoverable. These commands are precise when their starting state is precise; they are not a menu of interchangeable ways to make an error disappear.
# Context: PowerShell in the repository you intend to inspect.
git status
git diff
git diff --cached
git log --oneline --decorate -5
Select or unselect content
git add path selects the current content of that path for the next commit. Inspect git diff --cached afterward. In a repository with an existing commit, git restore --staged -- path removes the selected difference from staging while preserving working content. It does not delete the file. Review the state again before committing. git restore
For the Chapter 5 harmless-output exercise, use the exact path build-note.tmp. Then add a narrow ignore entry. An ignore rule applies to matching untracked files; it does not erase tracked content or earlier history. If the artifact was already committed, stop and plan that different cleanup rather than assume an ignore entry solved it.
Discard one unwanted working edit
From a clean baseline, after making one explicitly unwanted unstaged README edit, inspect git diff -- README.md. If you intend to lose that edit, git restore -- README.md restores the working file from the index by default. The index equals the baseline in this prepared example. If useful staged or working differences exist, inspect them before using this recipe.
Avoid a whole-directory restore when the decision concerns one path. Copy any valuable text to a named safe location first or preserve it in an appropriate commit. Git does not promise to recover an editor buffer that was never saved or content that was never recorded.
Reverse an isolated committed change
Use git show to inspect the actual commit. In the prepared FAULT-hysteresis exercise, the tip is an isolated faulty comparison. Run its test to reproduce the failure, then git revert --no-edit HEAD after confirming that HEAD is the intended commit. Revert adds a new corrective commit; the original remains in history. Rerun the same relevant tests and cross-build. git revert
If later work overlaps the reversal, a conflict can occur. Inspect the conflict and intended behavior rather than choosing a side mechanically. A merge commit has additional parent semantics and is outside this beginner recipe; do not apply an unexplained mainline option to a shared merge merely to make a command proceed.
Resolve or leave a merge in progress
Begin merges from a clean, understood state. When a text conflict occurs, read both contributions and write the intended final content. Remove markers, stage the resolved file, inspect the cached diff, and complete the merge commit. If you are not ready to resolve, git merge --abort is the documented route for abandoning the merge in progress. Its ability to reconstruct preexisting uncommitted changes is limited, which is why the clean start matters. git merge
Remote ahead
Preserve local work, run git fetch origin, and inspect git log --oneline --graph --decorate --all -8. If the local branch can simply advance, git pull --ff-only handles that case. Divergent histories require an integration decision. Do not treat force-push as a generic synchronization remedy. Verify the remote URL separately if the destination is wrong.
Advanced inspection: find a reference you moved
The reflog records local reference updates and may help locate a commit that is no longer named by the branch you expected. git reflog --date=iso is an inspection step. Read the relevant entry, inspect the candidate with git show, and create a new recovery branch at the verified commit if appropriate. Reflogs are local and subject to expiry; they are not a backup guarantee or a substitute for preserving work. git reflog
If you use a stash to set aside work, understand which files were included. The default does not necessarily include untracked or ignored artifacts. git stash list and git stash show help inspect recorded entries. Prefer applying and checking an entry before dropping it when preservation matters. The core chapters use explicit disposable folders and commits instead of relying on a stash for every interruption. git stash
The recovery is complete when the required behavior is restored, useful work remains accounted for, and the resulting history is understandable. A clean status alone cannot establish those three conditions.