Short answer: To git undo last commit while keeping your changes, run git reset --soft HEAD~1; use git reset HEAD~1 to also unstage them, or git reset --hard HEAD~1 to discard them. If the commit is already pushed to a shared branch, use git revert HEAD instead, which adds a new commit that reverses it safely.

Key takeaways

  • git reset moves the current branch back to an earlier commit.
  • Often you do not want to undo the commit at all, only fix it.
  • When teammates or a CI pipeline already have the commit, rewriting history causes conflicts and lost work for them.
  • Sometimes rewriting is right, for example on your own feature branch, or when you pushed a secret that must not stay in history.

I use these commands every week, on my own projects and when helping clients whose repositories got into a strange state. The command you need depends on two questions: has the commit been pushed and pulled by others, and do you want to keep the changes? Below is a quick decision table, then each case with exact commands and what they do to your files, plus how to get everything back with reflog if you go too far.

Git undo last commit decision flow: pushed or not, keep or discard changes, then reset, amend or revert
Decision table for undoing the last Git commit: reset soft, mixed or hard for local commits, amend for small fixes, and revert for pushed commits.

From my own work: Before any reset I run git status and git log –oneline -5, so I know exactly where I am. It takes five seconds and has saved me from resetting the wrong branch more times than I want to admit, usually late in the evening.

Which command should you use?

Situation Command What happens to your changes
Not pushed, want to recommit differently git reset --soft HEAD~1 Kept and staged
Not pushed, want to rework files first git reset HEAD~1 Kept in files, unstaged
Not pushed, want to throw the work away git reset --hard HEAD~1 Deleted (recoverable with reflog for a while)
Wrong message or forgotten file git commit --amend Kept, commit replaced
Pushed to a shared branch git revert HEAD Reversed by a new commit, history kept
Committed on the wrong branch git branch plus git reset --hard HEAD~1 Moved to the new branch

Undo the last commit with git reset

git reset moves the current branch back to an earlier commit. HEAD~1 means one commit before the current one. The mode decides what happens to the index (staging area) and your working files:

# Undo the last commit, keep the changes staged
git reset --soft HEAD~1

# Undo the last commit, keep the changes in your files but unstaged (default --mixed)
git reset HEAD~1

# Undo the last commit and DISCARD its changes completely
git reset --hard HEAD~1
  • --soft only moves the branch. Everything from the undone commit stays staged, ready for a new commit.
  • --mixed, the default, also resets the index. Changes stay in your files but are unstaged.
  • --hard resets the branch, the index and your working files. Uncommitted changes in tracked files are lost too, so commit or stash anything you need first.

To undo several commits, change the number: git reset --soft HEAD~3 undoes the last three and keeps all their changes staged, which is a quick way to squash local work into one clean commit.

Fix the last commit with git commit –amend

Often you do not want to undo the commit at all, only fix it. git commit –amend replaces the last commit with a new one:

# Fix the message of the last commit
git commit --amend -m "Fix login redirect after password reset"

# Add a forgotten file to the last commit, keep the message
git add src/forgotten.js
git commit --amend --no-edit

Amend creates a commit with a new hash. That is harmless locally, but it rewrites history if the old commit was already pushed.

Undo a pushed commit with git revert

When teammates or a CI pipeline already have the commit, rewriting history causes conflicts and lost work for them. git revert instead creates a new commit with the opposite changes:

# Create a new commit that reverses the last one (safe for shared branches)
git revert HEAD
git push

# Revert an older commit by its hash
git revert 3f2a9c1

# Revert a merge commit, keeping the first parent (the branch you merged into)
git revert -m 1 <merge-commit-hash>

For a merge commit, -m 1 tells Git which parent is the mainline. Be aware that after reverting a merge, merging the same branch again will not bring those changes back automatically; you would need to revert the revert. If a bad commit is buried deeper in history and you do not know which one, my guide to git bisect finds it quickly.

From my own work: On client repositories with automatic deployments, I always revert instead of resetting main, even if I pushed the commit only a minute ago. A revert is visible, deploys like any other commit, and nobody’s local copy breaks. Force-pushing main has caused more incidents in teams I have joined than the original bad commit.

If you must rewrite a pushed commit

Sometimes rewriting is right, for example on your own feature branch, or when you pushed a secret that must not stay in history. Reset locally, then push with --force-with-lease, which refuses to overwrite the remote if someone else pushed in the meantime, as documented in git push:

# Only if nobody else has pulled the commit yet
git reset --hard HEAD~1
git push --force-with-lease origin main

If the commit contained a password or API key, removing the commit is not enough: the secret has been exposed and must be rotated immediately. My guide to .env files and secrets best practices explains how to keep secrets out of commits in the first place.

Move the last commit to a new branch

A very common mistake is committing on main when you meant to start a feature branch. You do not need to undo and redo the work:

# You committed to main but meant to start a feature branch
git branch feature/invoice-export     # new branch points at your commit
git reset --hard HEAD~1               # move main back one commit
git switch feature/invoice-export     # continue working there

If main was already pushed with that commit, revert it on main instead and keep the branch.

Remove a single file from the last commit

# Remove one file from the last commit but keep it on disk
git reset --soft HEAD~1
git restore --staged config/local.env
git commit -c ORIG_HEAD

# Unstage a file before committing (not a commit undo)
git restore --staged path/to/file

git commit -c ORIG_HEAD reuses the original message and lets you edit it. Add the file to .gitignore if it should never be committed, such as local environment files.

Recover lost work with git reflog

Reset too far, or used --hard by accident? git reflog records where HEAD has been, including commits that no branch points to any more:

# Every position HEAD has been in, newest first
git reflog
# a1b2c3d HEAD@{0}: reset: moving to HEAD~1
# 9f8e7d6 HEAD@{1}: commit: Add invoice export

# Go back to the state before the reset
git reset --hard 9f8e7d6
# or keep it on a new branch
git branch rescue-invoice-export 9f8e7d6

Reflog is local to your repository and entries expire after a while, by default 90 days for reachable entries and 30 days for unreachable ones, so recover soon. It cannot restore changes that were never committed or stashed.

From my own work: When a client tells me they “lost a day of work” after a hard reset, the first thing I run is git reflog. In almost every case the commit is still there, one line down. Uncommitted edits are the real loss, which is why I commit small and often, even with messy messages I squash later.

Tips for Windows, the first commit and safety

  • Windows cmd: the caret is an escape character, so HEAD^ can misbehave. Use HEAD~1 or quote it as "HEAD^".
  • Undo the very first commit: there is no HEAD~1, so use git update-ref -d HEAD; your files stay staged.
  • Stash before hard resets: git stash saves uncommitted work you might want later.
  • Protect main: branch protection on your Git host blocks force pushes; my guide to GitHub Actions security hardening covers repository settings worth enabling.
  • Read the Pro Git chapter on undoing things for more background.

Git undo last commit checklist

  • Check git status and git log --oneline -5 first.
  • Not pushed and keep changes: git reset --soft HEAD~1 or git reset HEAD~1.
  • Not pushed and discard: git reset --hard HEAD~1 after stashing anything important.
  • Small fix: git commit --amend.
  • Pushed and shared: git revert HEAD.
  • Rewriting your own pushed branch: --force-with-lease, never plain --force.
  • Pushed a secret: rotate it, then clean history.
  • Lost something: git reflog straight away.

Want this done on your own server or site?

I am Saizul Amin, a web developer and technical SEO consultant. I set up and harden VPS servers, migrate WordPress sites, fix indexing problems and build automations. See my portfolio or get in touch.

FAQ

How do I undo the last commit but keep my changes?

Run git reset –soft HEAD~1 to keep the changes staged, or git reset HEAD~1 to keep them in your working files but unstaged. The commit disappears from the branch history and your code stays exactly as it was.

How do I undo a commit that I already pushed?

On a shared branch, use git revert HEAD and push. It adds a new commit that reverses the old one without rewriting history. Only use git reset and git push –force-with-lease if nobody else has pulled the commit.

What is the difference between git reset and git revert?

git reset moves the branch pointer back, removing commits from history, which is fine for local work. git revert keeps history and adds a new commit that undoes the changes, which is safe for commits other people already have.

Can I recover a commit after git reset –hard?

Usually yes. git reflog lists recent positions of HEAD. Find the hash from before the reset and run git reset –hard with that hash, or create a branch at it. Unreachable commits are eventually removed by garbage collection, so act soon.

How do I change the message of the last commit?

Run git commit –amend -m “new message”. If the commit was already pushed to a shared branch, amending rewrites history, so prefer leaving it or coordinate a force push with your team.

About the author. Saizul Amin is a web developer and technical SEO consultant from Dhaka who manages Ubuntu VPS servers, nginx, Cloudflare, WordPress and n8n in production for his own and client sites. Every command in these guides is one he runs himself; his open-source tools are on GitHub and his client work is at saizul.com.

Sources

NEW BLOGS. REAL STRATEGIES. REAL RESULTS.

Join our community and receive powerful SEO tips, web optimization guides, and growth strategies as soon as they’re published.

Don’t just read blogs — stay ahead of the competition.

We don’t spam! Read our privacy policy for more info.

Share.

Comments are closed.