When working with version control, making mistakes is part of the development process. Whether you committed to the wrong branch, forgot to add a file, or wrote an incorrect commit message, learning how to undo the most recent local commits in git is a critical skill for any developer.
In this guide, we will walk through the different methods to undo the most recent local commits in git using the git reset command, highlight safety precautions to prevent data loss, and show how to recover lost commits using the reflog.
Understanding git reset
The primary tool to undo local commits is the git reset command. This command allows you to move the current branch head (HEAD) back to a previous commit. Depending on the mode flag you pass to the command, Git will handle your staged changes and working directory files differently.
The 3 Modes to Undo Local Commits
Git provides three primary flags for resetting commits: --soft, --mixed (default), and --hard. Let’s explore how each one works.
1. Soft Reset (git reset –soft HEAD~1)
A soft reset moves the HEAD pointer back by one commit, but leaves your staging area (index) and working directory completely untouched. This means all changes from the undone commit remain staged and ready to be committed again.
git reset --soft HEAD~1Best Use Case: Use this when you want to edit the commit message, add missed files to the commit, or group (squash) multiple local commits into a single one before pushing.
2. Mixed Reset (git reset –mixed HEAD~1)
A mixed reset is the default behavior if you do not specify a flag. It moves the HEAD pointer back by one commit and unstages all changes, but keeps your files intact in your working directory. Your changes will appear as unstaged modifications.
git reset --mixed HEAD~1
// Or simply:
git reset HEAD~1Best Use Case: Use this when you want to undo the commit and select which files to stage and commit again from scratch.
3. Hard Reset (git reset –hard HEAD~1)
A hard reset moves the HEAD pointer back, unstages all changes, and completely discards all modifications in your working directory. Any local work since that commit is deleted.
git reset --hard HEAD~1WARNING: This command is destructive. If you have uncommitted changes in your working directory, they will be lost forever. Always check your status with git status before running a hard reset.
Git Reset vs. Git Revert
It is important to distinguish between git reset and git revert. You can read more about history management on the official Git Reset Book Chapter.
| Feature | git reset | git revert |
|---|---|---|
| Type of Operation | Alters commit history (moves pointers back) | Appends a new commit that rolls back changes |
| Safety Level | Safe ONLY for local commits not yet pushed | Safe for public and shared remote branches |
| Working Directory | Can overwrite files (in --hard mode) | Does not overwrite uncommitted files |
How to Recover from an Accidental Hard Reset
If you accidentally ran a hard reset and lost your commits, do not panic. Git keeps an internal record of all pointer changes called the **reflog**. You can view this log and recover your work:
- Run
git reflogto see the list of recent actions. - Locate the commit hash or reference (e.g.
HEAD@{1}) right before the reset occurred. - Restore your branch using:
git reset --hard HEAD@{1}
Summary
Undoing commits locally is a standard Git operation. Use --soft to keep changes staged, --mixed to unstage changes, and --hard to discard everything. Make sure you only reset commits that have not been pushed to a remote repository. For other developer workflows such as dynamic interface building, check out our tutorial on changing drop-down options with jQuery.
Leave a comment