Skip to main content

When to use reset, and when revert?

Short answer (the ideal interview answer)

  • git reset when you can rewrite history
  • git revert when rewriting history is not allowed

git reset - change history

git reset moves the branch pointer back, and by doing so changes the commit history.

Example:

bash
git reset --hard HEAD~1

What happens:

  • the last commit disappears from the branch's history
  • the history looks "as if it never happened"

When to use reset

  • local commits
  • the branch has not been pushed yet
  • you are working alone

Typical cases:

  • an accidental commit
  • a commit with junk
  • you need to "roll back and forget"

Dangerous:

  • on shared branches
  • after a push

git revert - undo with a new commit

git revert does NOT change history; instead it adds a new commit that undoes the changes of a previous one.

Example:

bash
git revert HEAD

What happens:

  • the old commit stays in the history
  • a new commit with the opposite changes appears

The history looks like this:

A --- B --- C --- D (revert C)

When to use revert

  • public branches (main, develop)
  • commits that are already pushed
  • team work

This is a safe way to roll back.


The key difference (make sure to say this)

reset erases commits from history, revert preserves history and simply adds an undo.


Table for clarity

Criterionresetrevert
Changes historyYesNo
Deletes commitsYesNo
Adds a new commitNoYes
Suitable for public branchesNoYes
Dangerous after pushYesNo

A frequent mistake candidates make

  • "reset and revert are the same thing" - wrong
  • "revert deletes the commit" - wrong
  • revert does not delete anything

Short answer for an interview

git reset is used to roll back local commits that have not been published yet, because it rewrites history. git revert is used on shared branches, because it does not change history but adds a new commit that undoes the changes.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.