Git Undo Last Commit (Keep Changes Staged)
Safely undo the previous commit without losing any written code or modifications.
Staff DevOps & Infrastructure Architect
Verified Command (Single-Click Copy)
Updated: 2026-10-30git reset --soft HEAD~1đ Prerequisites
State Changing / Caution- âGit CLI 2.0+
đĄ Primary Use Cases
Use --soft to preserve file changes while allowing you to edit .gitignore.
# How It Works
Moves the HEAD pointer back by 1 commit while leaving all your modified files in the Staging area (index), ready to be committed again.
Expected Terminal Output
[detached HEAD] Changes staged for commit:
modified: src/app.ts
modified: package.jsonGotcha & Common Pitfall
NEVER use `--hard` unless you deliberately want to permanently delete all changes made in that commit.
Performance & Complexity
âšī¸ Modifies local git commit graph history.
Environment & Version Support
Master Modern DevOps & CI/CD
Accelerate your career with industry-recognized Git, GitHub Actions, and Kubernetes certifications.
# Alternative Methods & Variations
Keep changes unstaged
Leaves changes in your workspace but unstaged.
git reset HEAD~1Amend commit message only
Change the commit message of the last commit without resetting.
git commit --amend -m "New commit message"đ People Also Ask (Related Quick Answers)
git restore --staged <file>git reflogFrequently Asked Questions about Git Undo Last Commit (Keep Changes Staged)
What is the difference between git reset --soft and --mixed?âŧ
--soft leaves changes in the staging area (git status shows green). --mixed (default) keeps changes in your working tree but unstages them (git status shows red).
Can I undo a commit if I already pushed to GitHub?âŧ
If pushed, prefer "git revert HEAD" to create a new inverse commit safely, or use "git push --force-with-lease" if on a private branch.