详解 Git 重置(Reset)操作:硬重置、软重置与混合重置 详解 Git Reset Operations: Hard, Soft, and Mixed Resets

In Git, "Reset" is used to undo or modify commit history by adjusting branch pointers and the state of the working directory/staging area. Beginners often make mistakes due to confusion about the different types. The following outlines the three common types and their core distinctions: **Soft Reset (--soft)** Only moves the HEAD pointer while preserving the working directory and staging area. Useful for modifying commit messages or re-staging changes for re-commit (e.g., `git reset --soft HEAD~1`). **Mixed Reset (default --mixed)** Moves the HEAD and resets the staging area, but retains the working directory. Ideal for undoing commits and reorganizing changes before re-committing (no parameters needed by default). **Hard Reset (--hard)** Moves the HEAD and completely resets the staging area and working directory, permanently discarding all modifications. Use only when certain changes are unnecessary (e.g., `git reset --hard HEAD~1`), as this operation is irreversible. **Key Distinctions**: - Reset modifies local commit history (suitable for unshared changes). - Revert creates new commits (use when changes have been pushed). - Hard reset requires caution: always confirm status with `git status` before use. Unbacked-up changes can sometimes be recovered via `reflog` (only applicable for unshared local commits). **Summary**: - Soft reset is lightweight for modifying history. - Mixed reset is the default and most commonly used. - Hard reset is dangerous and requires careful verification.

Read More