Sub topic 14
Decision guide: which undo command to use
This chapter assumes you know the three areas (working tree, index, and HEAD), that a branch is a pointer to a commit, and the basics of pushing to a remote. Here you will put the earlier commands into one decision process.
Ask two questions first
Two facts decide almost every undo.
- Where does the change live? It can be in the working tree, in the index, or in a commit.
- Has the commit been shared? A commit is shared once you have pushed it somewhere other people pull from.
Uncommitted changes are only on your machine, so you can change them freely. Commits that others have are different. Rewriting them forces everyone else to repair their history, so you add a new commit instead.
Quick reference
| Situation | Command | Rewrites history? | Can lose work? |
|---|---|---|---|
| Discard edits to a tracked file | `git restore file` | No | Yes, edits are gone |
| Unstage a file, keep edits | `git restore --staged file` | No | No |
| Delete untracked files | `git clean -fd` | No | Yes, never committed |
| Get an old version of a file | `git restore --source=HEAD~2 -- file` | No | Overwrites the file |
| Add to the last commit | `git commit --amend` | Yes | Rarely |
| Move the branch back | `git reset` | Yes | Only with `--hard` |
| Undo a pushed commit | `git revert` | No | No |
| Tidy local commits | `git rebase -i` | Yes | Possible |
Worked scenarios
You edited a file and want the old content back. The edits were never committed, so nothing else can restore them. Run git status to confirm the file, then git restore notes.txt.
You committed to the wrong point and have not pushed. Use git reset --soft HEAD~1 to keep the changes staged, or plain git reset HEAD~1 to keep them unstaged. Choose --hard only when you are sure you want the changes gone.
You pushed a bad commit to main. Other people may already have it, so use git revert a1b2c3d and push normally. For a merge commit, add -m 1.
You rewrote your own feature branch. After an amend, reset, or rebase, the remote branch no longer matches your local one. Push with git push --force-with-lease origin my-feature. The lease makes Git refuse the push if someone added commits you have not fetched.
You ran reset --hard and regret it. Run git reflog, find the earlier position of HEAD, and reset to it. This only works for work that was committed at some point.
Common mistakes
- Using
reseton commits that are already pushed. Your branch then disagrees with the remote, and a force push can erase teammates' work. - Running
git clean -fdwithout-nfirst. Untracked files are not in any commit, and the reflog cannot bring them back. - Using
git restore filewhen you meant--staged. The first discards your edits, and the second only unstages them. - Reaching for
--forceinstead of--force-with-lease.
Exercise
A teammate has already pulled your commit c9d8e7f from main, and it introduced a bug. Which command do you use, and why not git reset --hard HEAD~1 followed by a force push?
Show answer
Use git revert c9d8e7f and push normally. Revert adds a new commit that reverses the bug, so history only moves forward and your teammate's copy stays consistent. Reset plus a force push rewrites shared history. It would leave your teammate with a commit the remote no longer has, and it could discard other people's work.
Exercise
You staged config.py by mistake but want to keep your edits to it. Which command do you run?
Show answer
git restore --staged config.py. It removes the file from the index and leaves the working tree unchanged.