Skip to main content

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 Reset

Git 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 Revert

Git Reset and Git Revert compared

AspectGit ResetGit Revert
What it doesMoves the branch pointer to an earlier commitAdds a new commit that reverses an earlier one
HistoryRewritten: later commits drop off the branchPreserved: the original commit stays in the log
Safe on shared branchesNo, collaborators end up with conflicting historyYes, it is an ordinary new commit
Working filesKept or discarded, depending on --soft, --mixed or --hardChanged only by applying the reverse of the commit
Which commitsEverything after the target commitAny single commit or range, even an old one
After pushingNeeds a force push to update the remoteA normal push works
Best forCleaning up local commits before sharingUndoing 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

Git Resetbash
# 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
Git Revertbash
# 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 push

Readers 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.

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings