Side by side
Git ResetvsGit Revert
What is the difference between git reset and git revert?
Updated 2 min read7 differences
In short
Git reset moves a branch back to an earlier commit and drops the later ones, while git revert adds a new commit that undoes one, safe on shared branches.
Git Reset
Git reset is a Git command that moves the current branch back to an earlier commit and, depending on its mode, also resets the staging area and your files.
Read the page on Git ResetGit Revert
Git revert is a Git command that undoes an earlier commit by creating a new commit with the opposite changes, leaving the existing project history intact.
Read the page on Git RevertGit Reset and Git Revert compared
| Aspect | Git Reset | Git Revert |
|---|---|---|
| What it does | Moves the branch pointer to an earlier commit | Adds a new commit that reverses an earlier one |
| History | Rewritten: later commits drop off the branch | Preserved: the original commit stays in the log |
| Safe on shared branches | No, collaborators end up with conflicting history | Yes, it is an ordinary new commit |
| Working files | Kept or discarded, depending on --soft, --mixed or --hard | Changed only by applying the reverse of the commit |
| Which commits | Everything after the target commit | Any single commit or range, even an old one |
| After pushing | Needs a force push to update the remote | A normal push works |
| Best for | Cleaning up local commits before sharing | Undoing a change that is already published |
The difference, explained
git reset moves the current branch pointer to another commit and, depending on the mode, can also change the staging area and your working files. git revert leaves existing history alone and creates a new commit whose changes are the exact opposite of the commit you want to undo.
The difference is whether history is rewritten. Reset removes commits from the branch, which is fine for local work nobody else has seen but breaks things for collaborators who already pulled those commits. Revert only adds to history, so it is the safe way to undo a change that is already on a shared branch like main.
Reset has three main modes: --soft keeps the undone changes staged, --mixed (the default) keeps them as unstaged edits and --hard throws them away. Revert can undo any commit, not just the latest, so a typical workflow uses reset to clean up local commits and revert to back out a bad change that was already pushed.
A common misconception is that git reset --hard deletes commits forever. Git keeps the old commits for a while, and git reflog can usually find and restore them; uncommitted changes discarded by --hard, however, are truly gone.
Which one should you use?
Choose Git Reset when…
- The commits exist only on your machine.
- You want to squash or redo recent local commits.
- You want to unstage files or discard local changes.
Choose Git Revert when…
- The commit has already been pushed to a shared branch.
- You want a clear record showing what was undone.
- You want to undo an older commit while keeping later ones.
Undoing the last commit each way
# Undo the last commit, keep its changes staged
git reset --soft HEAD~1
# Undo the last commit and discard its changes
git reset --hard HEAD~1
# Already pushed? The remote now needs a force push
git push --force-with-lease# Create a new commit that undoes a specific commit
git revert a1b2c3d
# History now shows both the commit and its revert
git log --oneline
# f9e8d7c Revert "Add broken cache layer"
# a1b2c3d Add broken cache layer
git pushReaders ask
Can you undo a git reset?
Usually, yes. git reflog lists where the branch pointed before, so you can reset back to that commit; only uncommitted changes lost with --hard cannot be recovered this way.
Should I use reset or revert on main?
Use revert. Shared branches like main should not have their history rewritten, and a revert commit undoes the change while keeping everyone's copies consistent.
Can you revert a revert?
Yes. Reverting the revert commit reapplies the original changes, which is a common way to bring a feature back after fixing it.