Quick answer: run git restore --staged <file> (modern Git) or
git reset <file> (works everywhere) to unstage a file you added by mistake, before it's
committed.

Unstage a single file
git restore --staged myfile.txt
# older/equivalent syntax:
git reset myfile.txt
This removes the file from the staging area only. Your edits are completely untouched on disk — the file just goes back to "modified but not staged" instead of "staged for commit."
Unstage everything
git restore --staged .
# or:
git reset
What if I want to discard the edits too, not just unstage?
git restore myfile.txt
Be careful: this actually throws away your uncommitted changes to that file and reverts it to the last committed version. Only run this once you're sure.
FAQ
Does git reset delete my changes?
Plain git reset <file> or git restore --staged never touch your working
copy — they only move the file out of the staging area. Only git restore <file>
(without --staged) or git checkout -- <file> discard actual edits.
What Git version do I need for git restore?
Git 2.23 (August 2019) or later. On older installations, git reset <file> does the
same job.
How do I unstage in a GUI client?
Most Git GUIs (VS Code's Source Control panel, GitHub Desktop, SourceTree) show a "-" or "unstage" button next to each staged file that runs the equivalent command for you.
This article explains and expands on the community answers to the Stack Overflow question “How do I undo 'git add' before commit?”, used under the CC BY-SA 4.0 license. Screenshot credit: Stack Exchange Inc.
No comments
Post a Comment