Git Reset Modes and Clean Preview: Choose Safely
Compare the three reset modes
Commit-level reset moves the current attached branch to a target commit. Soft leaves index and working tree unchanged; mixed (default) resets the index to the target but leaves the working tree; hard resets both to the target, discarding tracked changes and possibly removing obstructing untracked paths. In three independent clean lab repositories, A contains Tea and B contains Rice. Each has a rescue branch at B. The table shows HEAD/index/worktree after resetting to A: soft = Tea/Rice/Rice; mixed = Tea/Tea/Rice; hard = Tea/Tea/Tea. Never use hard reset as a generic repair. A rescue branch saves commits only; save all uncommitted content separately.
git status --shortgit log --oneline -2git branch rescue-before-resetgit reset --soft HEAD~1git status --shortgit diff --cached -- notes.txtRun only in reset-soft. HEAD points at A, Rice is staged, and the working copy is Rice. To compare mixed and hard, use the separate prepared fixtures in the guide, not this already-reset repository.
Inspect mixed reset without losing the working file
Start in reset-mixed, not reset-soft. Confirm the working tree is clean, save the original commit on a branch, and reset to the parent. The hard mode is explained above and tested only in the trusted disposable fixture script; you do not need to run it on your own work.
git status --shortgit branch rescue-before-resetgit reset --mixed HEAD~1git status --shortcat notes.txtRice remains in the working tree as an unstaged edit; HEAD and index contain Tea. The rescue branch still points to B.
Preview untracked deletion; do not delete
In clean-preview, scratch.txt and temp/output.txt are untracked; cache.log is ignored by a committed .gitignore. git clean -n previews eligible untracked removals; -d includes untracked directories. Ignored files are excluded by default. -x expands the scope to ignored files, so do not add it casually. Preview is not a backup. Actual clean deletion may be irreversible; no deletion command is required in this lesson.
git status --short --ignoredgit clean -ngit clean -ndgit status --short --ignoredThe previews list scratch.txt and, with -d, temp/; cache.log is not listed. All three files remain and status is unchanged. For shared commits use revert rather than moving the branch backwards.
Practice
For an accidental unstaged edit, unwanted staging, and a shared bad commit, select restore, restore --staged, or revert respectively; explain why reset --hard is unnecessary.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Git Reset Modes and Clean Preview: Choose Safely — knowledge check
Reason about undo operations, affected states and preservation of work.
After soft reset to A from clean B, what is preserved?
After mixed reset to A from clean B?
What is the risk of hard reset?
What does git clean -nd do?
Which operation is least destructive for unwanted staging only?