Mistakenly added a file to the staging area with git add
? Don’t worry — Git makes it easy to undo that action without affecting your actual file content. This is known as unstaging a file.
In this post, you’ll learn how to revert a git add
, whether you want to unstage one file, multiple files, or everything at once.
🧾 What Is the Staging Area?
In Git, the staging area is a place where you prepare your changes before committing them. When you run:
git add filename
You’re telling Git: “This file is ready to be committed.”
If you change your mind before committing, you can unstage it without discarding the actual changes in the file.
🛠️ 1. Unstage a Single File
To remove one file from the staging area:
git restore --staged filename
Example:
git restore --staged index.js
This keeps your file changes intact — it just removes the file from the staged list.
🔁 2. Unstage All Files
If you’ve added multiple files and want to unstage all of them:
git restore --staged .
Or use the older equivalent:
git reset
Both commands remove all files from the staging area without deleting or changing the content.
🧰 3. Unstage a File Using git reset
(Legacy Option)
Before git restore
was introduced, this was the standard way:
git reset HEAD filename
Example:
git reset HEAD README.md
This works the same way — it unstages the file but leaves your code changes untouched.
🔍 Check the Status Before and After
Use git status
to see what’s staged and what’s not:
git status
You’ll see files in either the staged or unstaged section depending on their current state.
🧠 Summary
Task | Command |
---|---|
Unstage a single file | git restore --staged filename |
Unstage all files | git restore --staged . or git reset |
Legacy unstage (older Git versions) | git reset HEAD filename |
🏁 Conclusion
Accidentally staged the wrong files? No problem. Reverting a git add
is quick and safe — it doesn’t delete your work, it just gives you a chance to organize your commit properly.