Discard Unstaged Changes
Part of the Version Control section of Coddy's Terminal journey — lesson 40 of 58.
Sometimes you make edits in the working directory that you regret and want to throw away. As long as the changes have not been staged, you can roll the file back to its committed version with:
git restore notes.txtThis overwrites your working copy of notes.txt with the version from the latest commit. You lose your unsaved edits, but the staging area and history are unchanged.
If you want to throw away every unstaged change in the repo at once, use a dot:
git restore .git restore is destructive: anything you have not committed (or staged) is gone after this. Use it deliberately.
Challenge
BeginnerThe folder contains config.txt with the line port=80, already committed. Modify it to port=9999, then immediately discard the change with git restore. Print the final contents of the file.
Cheat sheet
Discard unstaged changes in a specific file (reverts to last commit):
git restore notes.txtDiscard all unstaged changes in the repo at once:
git restore .Warning: git restore is destructive — uncommitted and unstaged changes are permanently lost.
Try it yourself
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Version Control
2Getting Started
Initialize A RepositoryThe .git FolderConfigure Your IdentityGit StatusRecap - First Repo8Merging
What Is A MergeFast-Forward MergeThree-Way MergeMerge ConflictsResolve A ConflictRecap - Merge Master11Feature Branch Project
Project OverviewInitialize Main3Tracking Changes
The Staging AreaGit AddGit CommitModifying A Tracked FileGit LogRecap - First Commits6Recipe Site Project
Project OverviewInitialize And Ignore9Undoing Changes
Discard Unstaged ChangesUnstage A FileAmend The Last CommitRevert A CommitReset A BranchRecap - Time Machine