Git
gitgitReferenceIntermediate
git reset --soft
Undo recent commits while preserving all modified files in the staging area (index).
Verified: 2026-09-30
Tested on:git 2.44+
Platforms:
all
#git#reset#commits#staging
Command
git reset --soft HEAD~1
Short Description
git reset --soft moves the current branch HEAD pointer backward to a specified commit without touching your working tree or the staging area (index). All file changes from the undone commits remain staged and ready for a new commit.
Syntax
git reset --soft <commit-reference>Parameters
<commit-reference>: Target commit hash, branch name, or relative reference (e.g.HEAD~1for the parent commit,HEAD~3to undo the last 3 commits).
Examples
1. Undo Last Commit to Fix Commit Message or Staged Files
# Undo the last commit; changes stay staged in index
git reset --soft HEAD~1
# Make additional edits or re-stage
git add .
# Re-commit with a cleaner message
git commit -m "feat(auth): complete OAuth2 flow with token refresh"2. Squash Multiple Local Commits into One
# Move HEAD back 3 commits
git reset --soft HEAD~3
# All changes from the 3 commits are now staged together
git commit -m "feat(payment): implement Stripe checkout integration"Detailed Explanation: What Happens Under the Hood?
Git tracks state across three trees:
[ Working Directory ] <───> [ Staging Area (Index) ] <───> [ Commit History (HEAD) ]When you run git reset --soft HEAD~1:
- HEAD Pointer: Moves back by 1 commit.
- Staging Area (Index): Unchanged (contains the exact file diffs from the undone commit).
- Working Directory: Unchanged (no files on disk are modified or deleted).
Comparison of Git Reset Modes
| Reset Mode | Moves HEAD | Updates Index (Staging) | Updates Working Tree (Disk) | Safe for Unsaved Work? |
|---|---|---|---|---|
--soft | ✅ Yes | ❌ No (stays staged) | ❌ No (files untouched) | ✅ 100% Safe |
--mixed (default) | ✅ Yes | ✅ Yes (unstaged) | ❌ No (files untouched) | ✅ Safe |
--hard | ✅ Yes | ✅ Yes (cleared) | ✅ Yes (overwrites disk) | ⚠️ Destructive |
Common Options & Flags
| Flag | Description |
|---|---|
--soft | Keeps all modified files staged in the index. |
--mixed | Default behavior. Unstages changes but leaves files modified in working tree. |
--hard | Discards all changes in both the staging area and working directory. |
-q, --quiet | Suppresses summary output. |
When to Use
- When you committed too early and want to add more changes to the same logical commit.
- When you want to squash several micro-commits before opening a pull request.
- When you want to change the author, date, or commit structure of recent unpushed commits.
Warnings & Risks
Related Commands
git commit --amend— Quickly edit the previous commit message without resetting.git reset --mixed HEAD~1— Undo commit and unstage changes.git revert <hash>— Create a new inverse commit that safely cancels out a past commit.git reflog— Recover commits if you accidentally reset to the wrong commit reference.