⚡DevCheats
Cloudflare Edge
đŸŒŋVCS â€ĸ Git Version ControlVerified for Git 2.30+ - 2.45+âąī¸ 20 secondsđŸŽ¯ Beginner

Git Undo Last Commit (Keep Changes Staged)

Safely undo the previous commit without losing any written code or modifications.

Alex Chen
Alex ChenReviewed by Expert

Staff DevOps & Infrastructure Architect

📖3 min read
â€ĸ
📝490 words
â€ĸ
📅Updated 2026-10-30

Verified Command (Single-Click Copy)

Updated: 2026-10-30
bash
git reset --soft HEAD~1
Share snippet:𝕏in

📋 Prerequisites

State Changing / Caution
  • ✓Git CLI 2.0+

💡 Primary Use Cases

Accidental Commit with Wrong Branch or Staged Secrets:

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.json
âš ī¸

Gotcha & Common Pitfall

NEVER use `--hard` unless you deliberately want to permanently delete all changes made in that commit.

⚡

Performance & Complexity

Time ComplexityO(1)
Space (Memory)O(1)
Mutates OriginalYes (In-Place)

â„šī¸ Modifies local git commit graph history.

✓

Environment & Version Support

Git CLI
2.0+Verified
GitHub Desktop
AllVerified
Recommended Tool â€ĸ Coursera / Git

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.

bash
git reset HEAD~1

Amend commit message only

Change the commit message of the last commit without resetting.

bash
git commit --amend -m "New commit message"

🔍 People Also Ask (Related Quick Answers)

How to undo git add before commit?git restore --staged <file>
How to recover a deleted commit in git?git reflog
?

Frequently 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.

đŸ’ŦWas this snippet helpful for your problem?
← Previous Snippet

How to Drop a Column in Pandas DataFrame

Next Snippet →

How to Rename a Local and Remote Git Branch

Tags:#git#commit#reset#undo#vcs

More Git Version Control Snippets

View full Git Version Control cheatsheet →