Undoing and recovery
Discard working changes carefully
Restore a tracked file from the current commit when you truly do not need its uncommitted edits.
8 minute lesson
Discarding an uncommitted edit is destructive. Git cannot recover content that never reached the index or a commit.
First, inspect the file in both places:
git status
git diff -- config.js
git diff --staged -- config.js
If the staged diff is empty, the index matches HEAD. Restoring the working file will then bring it back to the current commit:
git restore config.js
By default, git restore copies from the index into the working tree. That is why checking the staged diff matters.
If you want to state the source explicitly, use:
git restore --source=HEAD --worktree config.js
Both commands overwrite working-tree edits in this scenario. If you might need the content, copy the file elsewhere or commit it on a temporary branch first.
Run git status and git diff -- config.js afterward. The working-tree change should be gone.
Exercise: make a disposable edit, inspect it, save a copy outside the repository, then restore the tracked file. Compare the restored file with your copy.
Lesson completed