Reset modes
In Git, the git reset command offers three primary modes (--soft
, --mixed
, and --hard
) for resetting the state of the current branch to a specified commit. Each mode provides different levels of reset, affecting the commit history, index (i.e., staging area), and working tree (i.e., working directory) in varying degrees:
- Mixed (default) mode restores the state of a changed index.
- Soft mode undoes a previous commit.
- Hard mode removes all traces of a commit.
Here is a quick breakdown of each reset mode.
Git reset modes in detail
Here is a more detailed breakdown of each mode.
- Soft Reset (--soft)
- Effect: Moves the
HEAD
pointer to the specified commit while preserving changes in the staging area and working directory. - Usage: Useful when you want to undo a commit but keep the changes staged for the next commit.
- Outcome: The commit is removed from the branch history, but the changes introduced by the commit remain staged. You can modify the staged changes and commit them again.
- Effect: Moves the
- Mixed Reset (Default Behavior)
- Effect: Resets the
HEAD
pointer to the specified commit and updates the staging area to match it, but does not affect the working directory. - Usage: Useful when you want to undo a commit and unstage its changes, allowing you to re-edit files before committing again.
- Outcome: The commit is removed from the branch history, changes are unstaged (but not lost), and files revert to the state of the specified commit.
- Effect: Resets the
- Hard Reset (--hard)
- Effect: Resets the
HEAD
pointer to the specified commit, resets the staging area to match it, and reverts all changes in the working directory to match the specified commit. - Usage: Useful when you want to completely discard all changes introduced after a certain commit.
- Outcome: The commit and all subsequent changes are removed from the branch history, staging area changes are discarded, and all files are reverted to the state of the specified commit.
- Effect: Resets the
Choosing the appropriate reset mode depends on whether you want to keep changes staged (--soft
), unstage changes (--mixed
), or discard changes entirely (--hard
). Always use caution, especially with --hard
, as it permanently removes changes and can lead to data loss if not used carefully.