How to Undo git add: Simple Commands to Unstage Files
To undo
git add and unstage files, use git restore --staged <file> for specific files or git restore --staged . to unstage all files. This removes files from the staging area without deleting changes in your working directory.Syntax
The command to undo git add is:
git restore --staged <file>: Unstages a specific file.git restore --staged .: Unstages all files currently staged.
Explanation:
git restore: A Git command to restore working tree files.--staged: Targets the staging area (index) instead of the working directory.<file>: The file path you want to unstage.
bash
git restore --staged <file> git restore --staged .
Example
This example shows how to unstage a single file and then all files after adding them.
bash
git add file1.txt file2.txt # Files are now staged git restore --staged file1.txt # file1.txt is unstaged, file2.txt remains staged git restore --staged . # All files are unstaged
Output
# After 'git add file1.txt file2.txt', both files are staged.
# After 'git restore --staged file1.txt', only file2.txt remains staged.
# After 'git restore --staged .', no files are staged.
Common Pitfalls
Many users try to use git reset to unstage files, which works but is less clear for beginners. Also, forgetting --staged will reset the working directory instead of just unstaging.
Wrong command example:
git restore file1.txt
This resets changes in the file, not just unstaging it.
Right command example:
git restore --staged file1.txt
bash
git restore file1.txt # This discards changes in file1.txt git restore --staged file1.txt # This unstages file1.txt but keeps changes
Quick Reference
| Command | Description |
|---|---|
| git restore --staged | Unstage a specific file |
| git restore --staged . | Unstage all staged files |
| git reset | Legacy way to unstage a file |
| git restore | Discard changes in working directory (not unstaging) |
Key Takeaways
Use
git restore --staged <file> to unstage files without losing changes.To unstage all files, run
git restore --staged ..Avoid using
git restore <file> if you only want to unstage, as it discards changes.The
git restore command is the modern, clear way to undo git add.Remember unstaging only removes files from the staging area; your edits remain in the working directory.