sequenceDiagram
participant Before as commit-id A
participant Working as Working Area
participant Staging as Staging(Index)
participant HEAD as commit-id B
Before->>Working: editing files
Working->>Staging: git add
Staging->>HEAD: git commit
HEAD->>Staging: git reset --soft HEAD~1
HEAD->>Working: git reset --mixed HEAD~1
HEAD->>Before: git reset --hard HEAD~1
Objective
What is git reset?
Definition: git reset
git reset is a command used to move HEAD and optionally unstage or delete changes.
Comparison Table between --soft, --mixed, and --hard
| Area | --soft |
--mixed (default) |
--hard |
|---|---|---|---|
HEAD (commit ptr) |
✔️ moved | ✔️ moved | ✔️ moved |
| Staging area (Index) | ✅ kept | ❌ cleared | ❌ cleared |
| Working dir files | ✅ kept | ✅ kept | ❌ cleared |
git reset sequence diagram
The following diagram is a step-by-step visualization of how different git reset modes affect Git’s three areas:
Do I need to run git reset --hard? Think again
When working with Git, you may find yourself needing to create a new branch from a previous commit becasue a bug was introduced in the most recent commit. For example, you might want to create a HOTFIX branch from one commit before the current HEAD.
You may be thinking of git reset --hard and git switch for checkout, but think again. In that case,
git switch -c HOTFIX HEAD~1is better.
What It Does
- Creates a new branch named
HOTFIXstarting from the commit beforeHEAD. - Switches you to the HOTFIX branch.
- Leaves your current branch and commit history unchanged.
Benefits
- Non-destructive: All commits are preserved.
- Clean separation: You work on the hotfix branch without altering your current one.
Best Practice Guideline Summary
| If you want to… | Use |
|---|---|
| Rewrite commit history but keep work | git reset --soft HEAD~1 |
| Unstage files, keep changes in files | git reset --mixed HEAD~1 (default) |
| Fully discard all changes and reset everything | git reset --hard HEAD~1 |
Create and switch to a new branch HOTFIX from HEAD~1 |
git switch -c HOTFIX HEAD~1 |