restore vs reset vs revert
Restore, Reset, and Revert
After this page you can pick the undo that matches what is wrong and who else has the commit.
Three audiences
uncommitted unpublished published
working tree or index only this clone has the hash someone else may have it
restore reset or amend revert
| Situation | Command | Branch tip |
|---|---|---|
| Edited a tracked file, not staged | git restore <file> |
stays |
| Staged too soon | git restore --staged <file> |
stays |
| Last local commit should be redone | git reset --soft HEAD~1, then commit |
parent; changes stay staged |
| Last local commit should be an unstaged edit | git reset HEAD~1 |
parent; changes stay in the worktree |
| Last local commit and its files should disappear | git reset --hard HEAD~1 |
parent; tracked files match the parent |
| Last local commit needs a new message or one more file | git commit --amend |
new hash, same parent (chapter 6) |
| A shared commit should be undone | git revert <hash> |
moves forward (chapter 10) |
restore copies bytes. The branch name stays. reset with no path slides the branch name. revert adds a commit.
Amend and soft reset
Both replace an unpublished tip.
Amend keeps one commit at the tip: same parent, new hash. You are still one commit ahead of the parent. Chapter 6 is amend.
Soft reset takes the tip off the branch and leaves that tree staged. You can drop a file, split the change, or commit it again with an ordinary message. The previous page is that mode.
Revert goes forward
git revert writes a new commit whose patch is the inverse of an older one. The old hash stays reachable. Use it when the commit is on a shared main, or when you are unsure whether anyone fetched it. Chapter 10 includes un-merging with git revert -m 1.
published: A ← B ← C ← R R undoes C; C is still reachable
local only: A ← B reset moved main off C; C is in the reflog
A test you can apply
Ask where the hash lives.
- In the working tree or the index only:
restore. - On this branch, and you have not pushed it (or there is no remote yet):
resetoramend. - On a forge, or a teammate has pulled:
revert.
Chapter 11 is the force-push that follows a reset of something you already pushed. On shared main, revert.