How to Undo the Last Commit in Git
Undo your most recent Git commit while keeping all your edited files staged or unstaged in your working directory.
git reset --soft HEAD~1Step-by-Step Execution Guide
git reset --soft HEAD~1Removes the commit from history but keeps all your modified files in the staging area ready to re-commit.
git reset HEAD~1Undoes the commit and unstages changes, keeping files modified in your working tree.
git reset --hard HEAD~1WARNING: Permanently deletes the commit and all file modifications made in it.
Critical Pitfalls & Precautions
- β’Never use "git reset --hard" if you have uncommitted work you want to save.
- β’If you have already pushed to GitHub/GitLab, rewriting history with "git reset" requires force pushing (git push --force-with-lease), which can disrupt teammates.
Alternative Approaches
git revert HEADCreates a new commit that inverts the previous commit, preserving shared branch history safely.
Undo Last Commit - Frequently Asked Questions
Common questions about safe rollback, remote history implications, and troubleshooting.
git reset --soft HEAD~1 only moves the HEAD pointer back, leaving your working files and staging area intact. git reset --hard destroys both the commit and any uncommitted modifications in your working directory.
Related Git Command Guides
Browse All Git RecipesDiscard all unstaged and uncommitted file modifications and restore your working tree to the latest commit.
Rename your current local branch and update the upstream remote tracking branch on GitHub or GitLab.
Delete a branch from remote origin (GitHub, GitLab, Bitbucket) after a pull request has been merged.
Amend the commit message of your latest commit before or after pushing.
Remove files from the Git staging area (index) without losing any of your local changes.
Temporarily shelve both modified and newly created untracked files to switch branches with a clean working copy.