To discard unstaged changes in Git (i.e., changes in your working directory that haven’t been added to the staging area), use the following commands:
1. Discard All Unstaged Changes
git checkout -- . # Discard changes to all tracked files
# OR (modern Git versions)
git restore . # Same effect as `git checkout -- .`
2. Discard Changes to a Specific File
git checkout -- path/to/file # Discard changes to a single file
# OR
git restore path/to/file
What This Does
git checkout -- .
orgit restore .
:- Reverts all tracked files in the current directory and subdirectories to their last committed state.
- Untracked files (new files not added to Git) are not affected.
Example Workflow
# 1. Check the status of unstaged changes
git status
# 2. Discard all unstaged changes
git restore .
# 3. Verify the changes are gone
git status
Key Notes
- Irreversible Action: This permanently deletes unstaged changes. Use with caution!
- Untracked Files: To remove untracked files, use
git clean -df
. - Staged Changes: If changes are staged (added to the index), use
git reset
first.
Related Commands
- Discard staged changes (undo
git add
):
git reset HEAD . # Unstage all changes (keeps files modified)
git restore --staged . # Modern equivalent
- Discard both staged and unstaged changes:
git reset --hard HEAD # Reset everything to the last commit
By using these commands, you can cleanly discard unwanted changes and reset your working directory.