Sub topic 12
Reset vs revert vs restore
This chapter assumes you can use init, add, commit, status, and log. It also assumes you know how git reset moves the branch, the index, and the working directory in its --soft, --mixed, and --hard modes. Here you will compare reset with two other commands that undo work, git revert and git restore. You will also see where git checkout fits. By the end you should be able to pick the right command for a given situation.
Three commands, three jobs
Git has several ways to undo work. They differ in what they change and whether they rewrite history.
git resetmoves the current branch to another commit. Depending on the mode, it also rewrites the index and the working directory. When it moves the branch backward, it rewrites history.git revertcreates a new commit that applies the inverse of an earlier commit. It never removes anything from history.git restorecopies file contents from a source into the index, the working directory, or both. It never moves the branch.
The key question is which of Git's areas you want to change: the branch history, the index, or the working directory.
| Command | Moves the branch? | Changes the index? | Changes the working directory? | Rewrites history? |
|---|---|---|---|---|
| `git reset --soft <commit>` | Yes | No | No | Yes, if the target is older |
| `git reset <commit>` | Yes | Yes | No | Yes, if the target is older |
| `git reset --hard <commit>` | Yes | Yes | Yes | Yes, if the target is older |
| `git revert <commit>` | Yes, forward, by adding a commit | Yes | Yes | No |
| `git restore <path>` | No | No | Yes | No |
| `git restore --staged <path>` | No | Yes | No | No |
A shared starting point
The examples below use the three-commit repository from earlier in this tutorial. If you no longer have it, recreate it.
git init demo && cd demo
echo one > file.txt
git add file.txt
git commit -m "first"
echo two >> file.txt
git commit -am "second"
echo three >> file.txt
git commit -am "third"
git log --onelineThe history is a straight line: first, second, third, with main (or master, depending on your Git configuration) pointing at third.
Revert: undo by adding history
Suppose the second commit contained a mistake. If you have already pushed it, other people may have built on it. Moving the branch back would remove commits they have. Instead, git revert writes a new commit that undoes the change.
git revert HEAD~1This asks Git to reverse the changes introduced by second. In this repository, third edited the line after the one second added, so the revert may conflict. Git then stops and asks you to resolve the conflict. Fix the file, run git add file.txt, and then run git revert --continue.
After a successful revert, the log looks like this:
a1b2c3d Revert "second"
9f8e7d6 third
5c4b3a2 second
1a2b3c4 firstAll three original commits remain. The new commit sits on top and undoes the effect of second. Anyone who already has third can pull this commit with an ordinary fast-forward.
Some practical points about revert:
- You can revert any commit in the history, not only the latest one.
- You can revert several commits with a range, for example
git revert HEAD~3..HEAD. This creates one revert commit per original commit. - Use
--no-commit(or-n) to apply the inverse changes to the index and working directory without committing. This lets you combine several reverts into one commit. - To revert a merge commit, you must tell Git which parent is the mainline with
-m, for examplegit revert -m 1 <merge-commit>. - A revert has a cost that surprises people. If you later merge the branch that contained the original change, Git treats it as already applied and then reverted. The change does not come back unless you revert the revert.
Reset: undo by moving the branch
Reset is the right tool when the commits exist only in your own repository. The three modes give you different amounts of cleanup.
git reset --soft HEAD~1
git statusThe branch moves back one commit. The changes from the removed commit stay staged.
git reset HEAD~1
git statusThe default --mixed mode also resets the index, so the changes remain in the working directory as unstaged modifications.
git reset --hard HEAD~1
git statusThe --hard mode overwrites the index and the tracked files in the working directory. The removed commit's changes are discarded from the working tree.
Compared with revert, reset leaves no trace in the branch history. That makes the history cleaner, but it is only safe when no one else has the commits you remove. If you reset a branch that others have pulled, their next push or pull will conflict with yours, because their history contains commits yours no longer has.
Restore: undo changes to files, not to history
git restore was added in Git 2.23 to split up the jobs that git checkout and git reset had been doing at the file level. It works on file contents only.
By default it restores the working directory from the index:
echo "oops" >> file.txt
git restore file.txtThe oops line is gone, and file.txt matches what is in the index. This discards the unstaged change, and Git does not keep a copy. Untracked changes cannot be recovered through the reflog, because they were never committed.
To unstage a file, use --staged:
echo "more" >> file.txt
git add file.txt
git restore --staged file.txt
git statusThe file leaves the index but keeps its modified contents in the working directory. This does the same thing as git reset -- file.txt, which you saw in the path reset chapter.
You can also choose a different source with --source:
git restore --source=HEAD~2 file.txtThis copies file.txt as it was two commits ago into the working directory. The branch and the index stay as they are. To update both the index and the working directory, combine the options:
git restore --source=HEAD~2 --staged --worktree file.txtBecause restore never moves the branch, it cannot lose commits. It can still lose uncommitted work in the working directory, so check git status and git diff first.
Where checkout fits
Before Git 2.23, git checkout handled both switching branches and restoring files. Both forms still work.
git checkout -- file.txt
git checkout HEAD~2 -- file.txtThe first form restores a file from the index. The second form restores it from HEAD~2 and also writes it to the index. This second form differs from git restore --source=HEAD~2 file.txt, which only updates the working directory unless you add --staged. If you read older tutorials, translate their checkout -- commands to restore. For branch switching, the newer git switch is the matching command. Use checkout mainly when you need to read or maintain existing scripts.
Choosing the right command
The decision depends on three questions. Have the commits been shared? Do you want the history to show the undo? Are you changing commits or only files?
Use these situations as a guide:
- You committed a bug and pushed it to
main. Usegit revert. Others keep a consistent history. - You made three messy commits on a local branch and have not pushed. Use
git reset --soft HEAD~3and commit again. - You staged the wrong file. Use
git restore --staged <file>orgit reset -- <file>. Both do the same thing. - You edited a file and want the committed version back. Use
git restore <file>. - You want an old version of one file without touching history. Use
git restore --source=<commit> <file>. - You want to throw away all local work and match the last commit. Use
git reset --hard HEAD. Checkgit statusfirst.
Common mistakes
- Using reset on pushed commits. If you must rewrite shared history, such as after a mistake with a secret, coordinate with your team. Pushing the result requires
git push --force-with-lease. Prefer revert unless you have a strong reason. - Expecting
git revertto remove the data. A reverted commit remains in history. If it contained a password, reverting does not delete the password from the repository. - Using
git restore <file>to unstage. Without--staged, it changes the working directory, not the index. Your edits are lost. - Reverting with a dirty working directory. Git refuses to start a revert if the changes would overwrite your uncommitted work. Commit or stash first.
- Forgetting the path form of reset.
git reset --hard -- file.txtis not allowed. Git does not support--hardwith paths. Usegit restorefor that job.
Summary
Reset moves the branch and is for local history. Revert adds a new commit and is for shared history. Restore changes file contents and never moves the branch. When you are unsure, start with the command that cannot lose commits (revert or restore), and check git status before any command that overwrites your working directory.
Exercise
You pushed commit abc123 to a shared main branch, and a teammate has already pulled it. The commit introduced a bug. Which command should you use to undo it, and why is git reset --hard HEAD~1 the wrong choice?
Show answer
Use git revert abc123. It adds a new commit that undoes the change, so teammates can pull it normally. git reset --hard HEAD~1 would remove the commit from your branch only. Your teammate's history would still contain it, so the branches would diverge and pushing would need a force push that can overwrite other people's work.
Exercise
You ran git add notes.txt by mistake and want it out of the next commit, but you want to keep your edits in the file. Give two commands that do this, and say which command would delete your edits if you left out a flag.
Show answer
Run git restore --staged notes.txt or git reset -- notes.txt. Both remove the file from the index and keep the working directory contents. If you leave out --staged and run git restore notes.txt, Git restores the working file from the index and your edits are lost.
Exercise
After a git revert of an earlier commit, the log still shows the original commit. Explain why, and name the one situation where that is a drawback.
Show answer
Revert adds a new commit that applies the inverse change and leaves the original commit in place. This is a drawback if the original commit contained sensitive data such as a password, because the data stays in history. It can also cause trouble when you later merge a branch that contains the original change, since Git sees it as already applied and reverted.