Command guide

git reset: soft vs mixed vs hard, explained

By · Updated · 2 min read · Checked against the official Git docs

git reset moves your current branch back to an earlier commit. The mode decides what happens to the changes from the commits you undo: --soft keeps them staged, --mixed keeps them as unstaged edits, and --hard deletes them.

Quick answer

To undo your last commit but keep the code, run git reset --soft HEAD~1.

Syntax

git reset [--soft | --mixed | --hard] [<commit>]

<commit> is where you want the branch to point. HEAD~1 means one commit back, HEAD~3 three commits back, or you can paste a hash from git log --oneline. Leave it out and Git uses HEAD, which is how git reset alone unstages everything.

Soft vs mixed vs hard

Mode Branch pointer Staging area Files on disk Use it to…
--soft Moves back Kept Kept Redo a commit, or squash several into one
--mixed (default) Moves back Reset Kept Undo a commit and re-pick what to stage
--hard Moves back Reset Overwritten Throw away commits and changes completely

Examples

Undo the last commit, keep changes staged

git reset --soft HEAD~1

Your files and staging area look exactly as they did right before you committed. Fix what you need, then commit again.

Squash your last three commits into one

git reset --soft HEAD~3
git commit -m "Add login form"

Unstage everything without losing edits

git reset

Newer Git versions also offer git restore --staged ., which does the same thing with a clearer name.

Throw away the last two commits completely

git reset --hard HEAD~2
Warning: --hard deletes work. Uncommitted changes are gone for good. Never reset commits that are already pushed to a shared branch: anyone who pulled them will hit conflicts. Use git revert there instead.

Make your branch match the remote exactly

git fetch origin
git reset --hard origin/main

How to recover from a bad reset

Git keeps a log of everywhere HEAD has pointed, called the reflog. Commits you reset away are still there (by default for at least 30 days):

git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~2
# 9f8e7d6 HEAD@{1}: commit: Add login form   ← the one you want
git reset --hard 9f8e7d6

git reset vs git revert vs git restore

Command What it changes Rewrites history? Safe on shared branches?
git reset Where the branch points Yes No
git revert Adds a new commit that undoes an old one No Yes
git restore Files in your working directory or staging area No Yes (local only)

FAQ

What does git reset do?

git reset moves your current branch pointer back to an earlier commit. Depending on the mode, it also clears the staging area (–mixed) or overwrites your files (–hard).

What is the default mode of git reset?

–mixed. Running git reset HEAD~1 undoes the last commit and unstages its changes, but leaves your edited files alone.

Can I undo a git reset –hard?

Committed work, usually yes: run git reflog, find the commit from before the reset, and run git reset --hard <hash>. Uncommitted changes wiped by –hard can’t be recovered by Git.

Should I use git reset or git revert?

Use reset for commits that exist only on your machine. Use git revert for commits already pushed to a shared branch, because revert adds a new commit instead of rewriting history.

Admin

Written by Admin

Writes practical Git guides for Git Commands.

Website