Sub topic 12
Reset vs revert vs restore
This chapter assumes you know git add, git commit, git log, and git status. It also assumes you know what the index and the working directory are, and how git reset moves a branch. Reset is one of three commands that "undo" something. Each one undoes a different thing, and picking the wrong one is a common source of trouble.
What each command changes
git resetmoves the current branch to another commit. Depending on the mode, it also updates the index and the working directory. It rewrites where the branch points.git revertcreates a new commit that applies the opposite of an earlier commit. It never moves the branch backward. History only grows.git restorecopies file contents from a commit or from the index into the index or the working directory. It does not move any branch.
| Command | Moves the branch? | Changes history? | Typical target |
|---|---|---|---|
| `git reset <commit>` | Yes | Drops commits from the branch | Local commits |
| `git revert <commit>` | No, adds a commit | No, adds to it | Commits already shared |
| `git restore <path>` | No | No | Individual files |
Worked example
Start from three commits, where file.txt contains one, two, and three on separate lines.
To undo the third commit and remove it from history, use reset. This is safe only if nobody else has the commit.
git reset --hard HEAD~1To undo the same commit while keeping history intact, use revert. Git records a new commit that removes the line three.
git revert HEADTo throw away an uncommitted edit to one file, use restore. The branch and the index are untouched.
echo oops >> file.txt
git restore file.txtTo unstage a file, you can use either command. Both leave the file contents alone.
git add file.txt
git restore --staged file.txtgit reset -- file.txt gives the same result. The restore form states the intent more clearly, because it names the file and the area it affects.
Choosing between them
Ask two questions.
- Has the commit been pushed to a branch others use? If yes, use
git revert. Reset would rewrite history that other people have already based work on. - Are you changing commits, or only files? If only files, use
git restore. If commits, useresetfor local cleanup orrevertfor a recorded undo.
A few common cases:
- Fix a typo in the last unpushed commit:
git reset --soft HEAD~1, then commit again. - Undo a bad commit on
mainthat teammates have pulled:git revert <hash>. - Discard unsaved edits in one file:
git restore <path>. - Unstage a file:
git restore --staged <path>orgit reset -- <path>.
Common errors
- Using
git reset --hardon a shared branch, then needinggit push --force. Teammates then have to repair their local copies. Usegit revertinstead. - Expecting
git revertto remove a commit from the log. The original commit stays, and a second commit cancels its effect. - Running
git restore --staged file.txtand expecting the working file to change. The--stagedoption only touches the index. - Reverting a merge commit without
-m. Git cannot tell which parent to keep, so you must pass-m 1(or the parent number you want).
Exercise
A teammate has already pulled your latest commit on main, and you find that it introduced a bug. Which command should you use to undo it, and why not git reset --hard HEAD~1?
Show answer
Use git revert <hash>. It adds a new commit that cancels the bug and keeps history intact, so your teammate's copy still fits with the shared branch. git reset --hard HEAD~1 would remove the commit from your branch only, so your history would diverge from your teammate's. You would then need a force push, which forces others to repair their repositories.
Exercise
You staged config.txt by mistake and you also have uncommitted edits in it that you want to keep. Which command unstages it without losing the edits? Name one that would lose them.
Show answer
git restore --staged config.txt (or git reset -- config.txt) unstages the file and leaves your edits in the working directory. git restore config.txt would overwrite the working copy with the index version, discarding your edits.