Undoing and recovery
Discard working changes carefully
Restore a tracked file from the current commit when you truly do not need its uncommitted edits.
Discarding an uncommitted edit is destructive. Git cannot recover content that never reached the index or a commit. I only use this when I am sure the edit is junk.
In notes-project, suppose you edited notes.txt with a bad paragraph you do not want. Inspect the file in both places before touching anything:
git status
git diff -- notes.txt
git diff --staged -- notes.txt
git status shows modified: notes.txt. git diff -- notes.txt shows the unwanted lines. If the staged diff is empty, the index matches HEAD. Restoring the working file brings it back to the current commit:
git restore notes.txt
Run git status afterward:
nothing to commit, working tree clean
git diff -- notes.txt prints nothing. The bad paragraph is gone from disk.
By default, git restore copies from the index into the working tree. That is why checking the staged diff matters first.
A common mistake: running git restore notes.txt when you meant to unstage. Without --staged, you hit the working tree. If you had good edits in the file and only wanted to remove them from the index, you just destroyed work. Read the command twice.
If you might need the content later, copy the file elsewhere before restoring:
cp notes.txt ~/notes-backup.txt
git restore notes.txt
If you staged notes.txt and also edited it again in the working tree, git diff and git diff --staged show different content. Restoring the working tree does not unstage the index. Use git restore --staged notes.txt when you only want to unstage.
Untracked files are a separate case. git restore only affects tracked paths. notes.local in notes-project is ignored, not tracked. To drop an untracked file, remove it from disk yourself or use git clean after reading its documentation carefully.
Try this: make a disposable edit to notes.txt, save a copy outside the repo, restore the tracked file, and compare the restored file with your copy.
Lesson completed