Learning Tips | Published Nov 17, 2026 | 8 min read

Git Reset vs Revert: When to Move a Branch or Add an Undo Commit

Compare Git reset and revert, understand soft, mixed, hard modes, and choose a safe way to undo local or shared commits.

Diagram contrasts reset moving a branch pointer backward with revert adding a new inverse commit to shared history

Git reset and git revert can both undo committed work, but they change history in different ways. This guide explains reset modes, when shared history calls for a revert commit, safe inspection steps, and how to handle merge commits without losing collaborators’ work.

When a commit contains a mistake, Git offers more than one way to correct the branch. git reset moves the current branch reference to another commit and can also change the staging area or working tree. git revert creates a new commit that applies the inverse of an earlier change. Choosing between them depends largely on whether the commit has already been shared.

Use reset to move your own branch pointer

On a private branch, reset is useful when you want to remove recent commits from that branch's visible history or move their changes back into the working tree for editing. For example:

# Move the branch back one commit; keep changes staged
git reset --soft HEAD~1

# Move the branch back one commit; keep changes, but unstage them
git reset --mixed HEAD~1

# Move the branch back one commit and discard tracked working-tree changes
git reset --hard HEAD~1

--soft moves HEAD while leaving the index and working tree unchanged. --mixed (the default mode) also resets the index, leaving file changes in the working tree. --hard updates the index and working tree to match the target commit; uncommitted changes to tracked files can be lost. Check git status and save anything important before using it.

Because reset changes the branch tip, resetting commits that teammates have already based work on can require a force push and disrupt their history. Even on a personal branch, a commit already used by another branch or deployment may be shared in practice. Treat “pushed” as a prompt to check who may depend on the commit, not as a magic rule that every pushed commit is impossible to reset.

Use revert to undo shared history transparently

Revert keeps the original commit in history and records a new commit that reverses its patch:

git revert <commit-sha>

This is usually the safer choice for a change already merged into a shared branch such as main. Teammates can fetch the same history without their branch pointers being rewritten. Review the resulting diff and commit message, resolve any conflicts carefully, and run the project's checks before pushing the revert.

A revert does not erase the original commit, and it may not be a perfect inverse if later commits depend on the change or edited the same lines. Git applies the inverse patch to the current tree, so conflicts or follow-up fixes may be needed. For a merge commit, Git needs a mainline parent selection, commonly supplied with -m; identify which parent represents the line of history to keep before attempting it.

Quick decision guide

SituationTypical choiceWhy
Undo your latest local commit but keep its staged editsgit reset --soft HEAD~1Moves the branch tip and leaves the index and files intact.
Undo a local commit but keep edits unstagedgit reset HEAD~1Default mixed reset moves the branch and resets the index.
Discard local committed and working changes intentionallygit reset --hard <target>Destructive for tracked changes; inspect and back up first.
Undo a commit already shared on a team branchgit revert <sha>Adds an auditable inverse commit without moving the shared branch backward.
Unstage one file without changing its contentsgit restore --staged <path>Changes the index, not the branch history or working file.

Inspect before and after

  1. Run git status and check whether there are uncommitted changes.
  2. Use git log --oneline --decorate -n 8 to identify the commit and current branch tip.
  3. Inspect the exact patch with git show <sha> before deciding what to undo.
  4. For reset, consider creating a temporary branch at the current tip so the old commit remains easy to find.
  5. Choose reset only when moving that branch pointer is acceptable; choose revert when history is shared and should remain intact.
  6. Review git diff and git status afterward, then run relevant tests before publishing changes.

Git often retains prior branch positions in the reflog for recovery, but recovery is not a substitute for a backup: reflog entries can expire, and destructive commands can remove uncommitted work. If you are unsure who has fetched a commit, prefer a revert or coordinate the history rewrite explicitly.

Do not confuse commit undo with file restore

Reset has several related forms. With a path, older git reset <path> syntax updates the index for that path; git restore --staged is clearer for unstaging. To discard or recover working-tree content, inspect git restore separately. Knowing whether you intend to change the branch pointer, index, or file contents helps avoid using a broad reset for a narrow task.

For the related choice of how to combine branch histories, see our guide to git merge versus rebase.

References

Want a structured Java learning path?

Explore Jaipur Engineers course options to connect Java fundamentals, debugging, backend development and project practice in a guided learning plan.

Explore the Java program   Ask for course guidance