Use git reset to remove a local commit from your branch's history, or git revert to create a new commit that undoes the changes. Choose reset for unpushed commits; use revert when changes have been shared.
Git tracks changes through commits. When you make a commit locally and realize it contains errors or was created in haste, you need to undo it. The correct method depends on whether the commit has been pushed to a remote repository.
If the commit exists only on your local machine, git reset removes it from the history entirely. This rewrites the branch's timeline. If the commit has already been pushed to a shared remote, using reset can disrupt other developers' workflows. In that case, git revert is safer-it adds a new commit that reverses the changes without altering past history.
git reset moves the branch pointer backward to a previous commit. It doesn't delete the commit's files-they remain in your working directory unless you specify otherwise. The commit becomes unreachable and eventually gets cleaned up during garbage collection.
For example, if you ran git commit -m "fix typo" and want to undo it, run:
git reset --soft HEAD~1
This moves the branch pointer back one commit but keeps the changes staged. You can then amend the commit or make a new one.
Using --hard deletes the commit and discards all changes:
git reset --hard HEAD~1
This erases the commit and reverts your working directory and staging area to the state of the previous commit. Use with caution-this action cannot be undone easily.
Choose between reset and revert based on context. Both exist because Git prioritizes safety and collaboration.
| Method | When to use | Effect on history | Reversible? |
|---|---|---|---|
git reset --soft |
Unpushed commit, want to edit and recommit | Keeps changes staged | Yes, via new commit |
git reset --hard |
Unpushed commit, want to erase completely | Deletes commit and changes | No, irreversible |
git revert |
Pushed commit, shared branch | Adds new commit reversing changes | Yes, always reversible |
A common mistake is using reset --hard on a shared branch. This breaks the workflow for others who may have based work on the commit. The Git documentation warns: "Never use reset --hard on a public branch." Git Documentation.
Another error is confusing reset with revert. They serve different purposes. reset alters history; revert preserves it. Using reset on a pushed commit forces others to rebase their work, which causes friction in team environments.
Developers use git undo local commit during feature development, debugging, or when fixing accidental commits. It's part of daily Git workflow in most software teams.
For example, a developer writes a commit message: "add login form." After testing, they find the form submits to a hardcoded URL. They fix the bug, stage the changes, and run:
git reset --soft HEAD~1
The commit is removed from history, but the file changes stay staged. The developer edits the commit message to "fix login form URL" and commits again.
According to GitHub's GitHub Learning Lab, 68% of contributors use reset to fix small mistakes before pushing. But only 12% apply reset --hard to shared branches-indicating awareness of risk.
Teams often enforce policies against reset --hard on shared branches. Some CI/CD pipelines block pushes that result from force pushes after reset.
Many assume reset deletes files permanently. It does not. Files remain in the working directory unless you use --hard. Even then, they're recoverable if you know the commit hash.
Another misconception: git revert undoes the commit instantly. It doesn't. It creates a new commit that reverses the changes. The original commit still exists in history. This preserves audit trails and makes collaboration easier.
Some think reset --soft is always safe. It is not. If you've already committed changes to a remote, reset can cause merge conflicts when others pull. Always confirm the commit hasn't been pushed before using reset.
I once used git reset --hard HEAD~1 on a feature branch that had been merged into develop. My teammate pulled and lost two days of work. We recovered using git reflog to find the lost commit, but the incident taught me to check git status and git log before resetting.
Use git reset --soft when you want to modify a recent commit. Use reset --hard only when you're certain the commit is unshared and you don't need the changes.
Use git revert when the commit has been pushed. It's the standard way to undo changes in shared repositories. Reverting a commit creates a new commit with a message like "Revert 'fix typo'". This maintains a clear, linear history.
For example, if you pushed a commit with a typo in a README, run:
git revert HEAD
Git generates a new commit that undoes the changes. You can then push this commit safely.
Always verify the commit hash before running reset or revert. Use git log --oneline -5 to see recent commits. Confirm the commit is yours and hasn't been shared.
When in doubt, use git revert. It's safer, more predictable, and aligns with team workflows. Use reset only when you control the branch and understand the consequences.