How to Use git reset --soft to Undo a Commit

git reset --soft HEAD~1 removes the last commit from history but keeps every file edit staged - so you can keep iterating without losing a single line of work. It's the right tool when you committed too early on a branch you haven't pushed yet.

AI Engineergitresetversion-control

The three resets - and why --soft is the safe one

Git's reset command moves HEAD to a different commit and gives you three options for what happens to the files that were in the commit you're undoing:

Flag Commit gone? Index (staged)? Working tree?
--soft yes stays staged unchanged
--mixed (default) yes unstaged unchanged
--hard yes cleared changes discarded

--soft is the "I committed too early" undo. The commit disappears from history, but every edit stays in your index ready for a fresh commit. --hard is irreversible without a reflog rescue - don't use it when you want to keep the work.

Undo the last commit

cd /workspace/repo

# see what's in history before the reset
git log --oneline
# e.g.: a3f9c12 WIP: experiment.py changes
#        b1e4d08 initial commit

git reset --soft HEAD~1

# the WIP commit is gone; edits are still staged
git log --oneline
# b1e4d08 initial commit

git status
# Changes to be committed:
#   modified: experiment.py

HEAD~1 means "one commit before HEAD." You can go further back - HEAD~2 undoes the last two commits, keeping all their changes staged together.

After the reset

Because the changes are staged, your options are:

When NOT to use git reset

git reset rewrites local history. If you have already pushed the commit to a shared branch, a reset followed by a force-push will rewrite history for everyone else on the branch - that breaks their local copies. For commits that are already pushed, use git revert instead: it creates a new commit that undoes the change without touching existing history.

# safe undo for pushed commits - creates a new "revert" commit
git revert HEAD

Want to try it hands-on? HeyDevJob gives you this exact setup in a live cloud workspace in your browser - edit it, run it, and see it work. Free, nothing to install.

Try it in a workspace →

What you'll practice

FAQ

What does git reset --soft do?

It moves HEAD back one commit (removing the commit from history) but leaves all the file changes staged in the index. Your edits are preserved and ready to re-commit - nothing in your working tree is touched.

What's the difference between git reset --soft, --mixed, and --hard?

--soft keeps changes staged; --mixed (the default) keeps changes but unstages them; --hard discards the changes entirely. Use --soft when you committed too early and want to keep editing or split the commit.

Can I use git reset --soft on a pushed commit?

You can run it locally, but pushing afterward requires --force, which rewrites shared history and breaks teammates' copies. For pushed commits, use git revert instead - it adds a new undo commit without rewriting history.

What does git reset --soft do?

git reset --soft HEAD~1 moves the branch pointer back one commit but leaves your changes staged. It undoes the commit, not the work - useful when you committed too early or want to recommit differently.

Is git reset --soft safe?

Yes - it is the safest reset because it keeps all your changes staged and deletes nothing. git reset --hard is the dangerous one, since it discards working-tree changes.

Keep learning

Restore a Broken LLM API IntegrationAI/ML projectHarden an LLM Pipeline Against API FailuresAI/ML projectParse JSON From an LLM (Strip Markdown Fences)AI/ML projectAI/ML roadmapStep by step to hiredAI/ML interview questionsSTAR answersAll AI/ML projectsProjects hub

Learn it by doing. Open this in a live cloud workspace, make the change yourself, and keep a record of the work you can share.

Open the workspace →