How to reset a commit in Git
Resetting commits in Git enables you to undo commits and move the branch pointer backward, providing control over commit history and working directory state for fixing mistakes and reorganizing changes. As the creator of CoreUI, a widely used open-source UI library, I’ve used git reset extensively across development workflows to fix commit errors, reorganize history, and prepare clean commits before sharing with the team. From my expertise, the most effective approach is using git reset with appropriate modes (soft, mixed, hard) based on whether you want to preserve or discard changes. This method provides flexible commit undoing with precise control over working directory and staging area states.
Use git reset with different modes to control how commits are undone and changes are preserved.
# Reset to previous commit, keep changes staged (--soft)
git reset --soft HEAD~1
# Reset to previous commit, keep changes unstaged (--mixed, default)
git reset HEAD~1
git reset --mixed HEAD~1
# Reset to previous commit, discard all changes (--hard)
git reset --hard HEAD~1
# Reset to specific commit by hash
git reset --soft abc1234
git reset --mixed abc1234
git reset --hard abc1234
# Reset multiple commits back
git reset --soft HEAD~3
# Reset to a specific branch state
git reset --hard origin/main
# Check what will be reset before doing it
git log --oneline -10
git show HEAD~1
# Reset and keep untracked files
git reset --hard HEAD~1
# Untracked files remain untouched
# Reset with path (reset specific files only)
git reset HEAD~1 -- file.txt
git reset --hard HEAD~1 -- src/
Git reset moves the current branch pointer backward and controls working directory state. Use –soft to keep changes staged, –mixed (default) to keep changes unstaged, or –hard to discard changes completely. HEAD1 refers to one commit back, HEAD3 goes three commits back. Always verify your target commit before resetting.
Best Practice Note:
This is the same git reset workflow we use in CoreUI development to maintain clean commit history and fix mistakes before pushing to shared repositories.