Suggest an editImprove this articleRefine the answer for “When to use reset, and when revert?”. Your changes go to moderation before they’re published.Approval requiredContentWhat you’re changing🇺🇸EN🇺🇦UAPreviewTitle (EN)Short answer (EN)**`git reset`** is used when history can be rewritten, and **`git revert`** when rewriting history is not allowed. **Key point:** `reset` erases commits from history, while `revert` preserves history and simply adds a new commit that undoes the changes.Shown above the full answer for quick recall.Answer (EN)Image## 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 | 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 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.For the reviewerNote to the moderator (optional)Visible only to the moderator. Helps review go faster.