When working with Git, you often use git add
to stage files before committing. But what if you accidentally staged the wrong file or want to exclude a file before committing? Fortunately, Git makes it simple to unstage files without losing your changes.
In this blog, we’ll cover how to remove a file from the staging area (i.e., undo git add
) using the command line.
What Does git add
Do?
Before we jump in:git add
tells Git to include changes in the next commit. The staged files are stored in an index called the staging area.
If you change your mind after running git add
, the file is still changed in your working directory—it just won’t be committed if you unstage it.
How to Remove a File from Git Add (Unstage a File)
1. Unstage a Specific File
If you want to unstage a single file:
git restore --staged path/to/your-file
Example:
git restore --staged README.md
This removes the file from staging but keeps your local edits intact.
Alternative (older method):
git reset HEAD path/to/your-file
2. Unstage All Staged Files
To remove all files from the staging area (unstage everything):
git restore --staged .
or
git reset HEAD
3. Check Your Staging Area
To see which files are staged or unstaged, use:
git status
Files in the “Changes to be committed” section are staged.
What Happens After You Unstage?
- The file stays modified in your working directory.
- It just won’t be included in the next commit.
- You can always stage it again later with
git add
.
Why Unstage Files?
- Accidentally staged the wrong file.
- Want to split your changes into multiple commits.
- Need to exclude temporary or sensitive files.
Summary Table
Task | Command |
---|---|
Unstage a specific file | git restore --staged <file> |
Unstage a specific file (older) | git reset HEAD <file> |
Unstage all files | git restore --staged . or git reset HEAD |
Check staged files | git status |
Final Thoughts
Removing files from the staging area is a common part of the Git workflow. Knowing how to unstage files without losing your work keeps your commits clean and purposeful.