When should I use Git revert instead of moving a branch backward?
Quick answer
Use git revert when you want to undo a commit’s changes by adding a new commit while keeping the original in history. Use reset when you intentionally want the current branch to point to an earlier commit. Reset’s effects on staged and working files depend on its mode; a hard reset makes tracked files match the target and may overwrite untracked files. For shared work, consider the coordination impact before changing the branch tip.
The question, the details, the next step.
A practical checklist
Confirm the current branch and exact commit.
Check whether others rely on the branch.
Preserve unfinished or untracked work you need.
Use revert to add an undo commit while retaining the original commit in history. [S1]
Before resetting, choose a mode based on the desired branch, index, and working-tree state. [S2]
Get guidance if the target is a merge commit or you are unsure. [S1]
A few more useful answers
Does `git revert` delete the original commit?
No. It adds a commit that reverses changes from the selected commit, leaving the earlier commit in history. [S1]
Does every reset mode change working files?
Reset modes differ in their effects on the index and working tree. A hard reset makes tracked files match the target and may overwrite untracked files. [S2]
Can I use the same simple revert command for a merge commit?
Do not assume so. Merge commits have special considerations; check project guidance or ask a maintainer. [S1]
Sources
References used for this guide.
- [S1] Git documentation: git-revert git-scm.com · Checked
- [S2] Git documentation: git-reset, version 2.53.0 git-scm.com · Checked
