When to use reset, and when revert?
Short answer (the ideal interview answer)
git resetwhen you can rewrite historygit revertwhen 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~1What 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 HEADWhat 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)
reseterases commits from history,revertpreserves history and simply adds an undo.
Table for clarity
| Criterion | reset | revert |
|---|---|---|
| Changes history | Yes | No |
| Deletes commits | Yes | No |
| Adds a new commit | No | Yes |
| Suitable for public branches | No | Yes |
| Dangerous after push | Yes | No |
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 resetis used to roll back local commits that have not been published yet, because it rewrites history.git revertis used on shared branches, because it does not change history but adds a new commit that undoes the changes.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.