Core idea
Git provides several undo-related commands because "undo" can mean different things. You may want to unstage a change, discard an uncommitted edit, recover a deleted file, remove a tracked file, undo the latest commit, or reverse an older commit without erasing history.
The correct command depends on what must change and what must remain.
How it works
- Unstage changes with git reset
A plain reset moves staged changes back to the working directory:
git resetThe file edits remain. Only their staged state is removed.
To undo the latest commit while returning its changes to the working process, the demonstrated command is:
git reset HEAD^This moves away from the latest commit and makes its changes available for staging or modification again.
- Discard working changes with git restore
Restore one file to its most recent committed state:
git restore 1.txtRestore a directory:
git restore my-folderRestore all working-directory changes in the repository:
git restore .This is appropriate when an uncommitted approach has failed and the desired result is the exact last committed version rather than a manual reconstruction.
If a change has already been staged, remove it from staging while preserving the working-directory content with:
git restore --staged 1.txtor:
git restore --staged .- Reset the repository state forcefully
A normal reset changes staging state but does not necessarily recreate deleted files in the working directory. To restore both repository state and working files to the committed state in the demonstrated workflow, use:
git reset --hardThis can bring deleted files back and remove local modifications, but it also discards uncommitted work.
- Remove tracked content with git rm
Delete a tracked file and stage that deletion in one step:
git rm 4.txtIf the file contains local modifications, Git may refuse because deleting it would discard uncommitted changes. If deletion is intentional despite those changes, force it with:
git rm -f 4.txtRemove the file from Git tracking while keeping the physical file in the working directory with:
git rm --cached 4.txtAfterward, Git reports the retained file as untracked.
Remove a tracked directory and its contents recursively with:
git rm -r my-folderThe deletion is staged automatically.
- Reverse a committed change with git revert
To cancel the effect of a previous commit without deleting it from history, use:
git revert <commit-id>Revert creates a new commit whose changes reverse the selected commit. The original commit remains visible, and the new revert commit records that a correction occurred.
This differs from reset. Reset can move the project back and remove later commits from the visible history. Revert preserves the existing history and adds a new corrective event.
Why it matters
Undo operations are high-risk when their scope is misunderstood.
The safest question is not "How do I undo this?" but "Which state should be preserved?"
- Preserve file edits but remove them from staging: git reset or git restore --staged.
- Discard uncommitted file edits: git restore.
- Restore committed files and discard all current local changes: git reset --hard.
- Delete and stage a tracked file: git rm.
- Stop tracking but keep the local file: git rm --cached.
- Correct a committed mistake while preserving a traceable history: git revert.
- Remove the latest commit and return its changes to active work: git reset HEAD^.
This decision framing prevents a destructive command from being used when a reversible one would be sufficient.
Practical application
Scenario A: A file was edited incorrectly but not staged.
git status
git restore 1.txt
git statusScenario B: Several changes were staged together by mistake.
git status
git reset
git statusThen stage only the intended files.
Scenario C: A tracked file should be deleted and included in the next commit.
git rm 4.txt
git status
git commit -m "Remove 4.txt"Scenario D: A local file should remain on disk but no longer be tracked.
git rm --cached 4.txt
git statusScenario E: An earlier commit introduced a mistake that should be visibly corrected.
git log --oneline
git revert <commit-id>
git log --onelineThe log now contains both the original commit and the later revert commit.
Trade-offs and limitations
Git reset --hard discards uncommitted modifications. It should be used only when losing those changes is intentional.
Git rm -f also discards local modifications in the removed file.
Git restore replaces uncommitted work with the last committed version. That is useful for abandoning a failed approach, but inappropriate when the work still needs to be preserved.
Reset can make commits disappear from the visible branch history. Revert is more traceable because it records the correction as a new commit.
Git rm --cached does not delete the local file. It changes tracking state, so the file then appears as untracked.
Key takeaway
Choose an undo command by identifying what must be kept: staging state, working files, commit history, or the physical file itself. Reset, restore, rm, and revert solve different problems and are not interchangeable.