Course contentsShow
← Computer Science
Lesson 2179 of 2,87249. Version Control with GitFree lesson

Unstaging Files

Remove files from staging area with 'git reset' or 'git restore --staged' without losing changes.

Unstaging Files

What you'll learn: How to remove files from Git's staging area without losing your actual changes.

The Problem

You've just used git add to stage some changes, but then you realize you added something by mistake—or you're not ready to commit it yet. You want to unstage the file, but you don't want to lose the work you've done.

Understanding the Staging Area

Think of Git's staging area like a loading dock. Files there are packed up and ready to ship (commit), but they haven't left yet. Unstaging means moving a file back off the loading dock while keeping the actual goods (your changes) safe in your working directory.

Two Ways to Unstage

Modern Approach: git restore --staged

git restore --staged filename.txt

This command specifically targets the staging area. It tells Git: "Take this file out of staging, but leave my working directory alone."

Traditional Approach: git reset

git reset filename.txt

This also removes the file from staging. git reset is more versatile (and older), but git restore --staged is clearer about what you're doing.

What Happens to Your Changes?

Nothing! Both commands leave your actual file modifications intact in your working directory. The file goes from staged back to modified (in Git's three states: modified, staged, committed). You can still see the changes with git diff, edit them further, or stage them again later.

To unstage everything:

git reset

Key Takeaway: Use git restore --staged <file> or git reset <file> to remove files from the staging area while preserving all your working directory changes—perfect for when you staged something too early or by mistake.