How to Undo the Last Git Commit Without Losing Your Code (Local and Pushed Commits)

How to Undo the Last Git Commit Without Losing Your Code (Local and Pushed Commits)

Tech Tutorials Programming Git

Committed the wrong files, used the wrong message or accidentally pushed a broken change? Git gives you several ways to correct the mistake. The right command depends on whether the commit has been shared and whether you want to keep its file changes.

1. Inspect your repository first

git status
git log --oneline -5
git branch -vv

Make sure you are on the intended branch. If unsure whether your latest commit is pushed, fetch the remote and inspect the branch comparison. Save unrelated uncommitted work before rewriting history. These examples assume the commit has a parent and is not a merge commit.

2. Undo a local commit and keep changes staged

git branch backup-before-undo
git reset --soft HEAD~1
git status

This moves the current branch back by one commit without changing the files or staging area. With an otherwise clean repository, the removed commit's changes are ready to commit again. The backup branch keeps a reference to the original commit. Edit as needed and recommit:

git add path/to/file
git commit -m "Corrected commit"

3. Undo a local commit and keep changes unstaged

git reset --mixed HEAD~1
git status
git diff

Mixed reset moves the branch back and resets the staging area while preserving working files. This is useful when you want to divide one large commit into several smaller ones. Files that were newly added in that commit may appear as untracked. Inspect them before using git add.

4. Only the commit message is wrong

For a commit that has not been shared, use:

git commit --amend -m "Better commit message"

Check your staged changes first, because amend can also include them. Amending changes the commit ID, so avoid casually amending a shared commit.

5. Undo a commit already pushed to a shared branch

git revert HEAD
git push

Revert creates a new commit that reverses the selected change without rewriting existing history. Start from an up-to-date branch and clean working tree. If conflicts occur, resolve them, stage the corrected files and run git revert --continue, or use git revert --abort to cancel. A merge commit needs separate mainline handling; do not blindly revert a merge.

6. Recover a commit after an accidental reset

git reflog
git branch recovered-work COMMIT_HASH

Replace COMMIT_HASH with the relevant hash you found. Inspect that branch before merging or cherry-picking its work. Reflog is local and entries can expire; it is not a backup service. It also cannot guarantee recovery of uncommitted files discarded by destructive commands.

Which command should you use?

GoalCommand
Keep staged changesgit reset --soft HEAD~1
Keep unstaged changesgit reset --mixed HEAD~1
Correct local messagegit commit --amend
Undo shared commitgit revert HEAD

Do not use git reset --hard or force-push just to correct an ordinary mistake. If a secret was committed, revoke or rotate it immediately; reverting alone does not remove it from history.

References

Git reset documentation | Git revert documentation | Community discussion: undo local commits