Unstage a File
git restore --staged config.php
The file leaves the staging area and will not be part of the next commit. Your edits stay in the file on disk: unstaging never changes the working tree. git restore arrived in Git 2.23, and it is the command git status suggests once the repository has a commit. Several files work too: git restore --staged a.php b.php.
Check What Is Staged First
git status
git diff --staged
git status lists staged files under "Changes to be committed" and edited but unstaged files under "Changes not staged for commit". git diff --staged shows the exact lines the next commit would contain. Run both before and after unstaging to confirm the result.
Unstage Everything
git restore --staged .
git reset
Either command empties the staging area. git reset without arguments sets the index back to the last commit and leaves your files alone. Do not add --hard: that form also throws away the edits in your files.
The Older Form: git reset HEAD
git reset HEAD config.php
git reset config.php
Both lines unstage one file, exactly like git restore --staged. You see this form in older tutorials and in scripts written before Git 2.23. It is still supported.
Unstage Part of a File
git reset -p config.php
Git shows each staged hunk and asks what to do. Answer y to unstage that hunk, n to keep it staged, or q to stop. This helps when one file holds a fix you want to commit and a debug line you do not.
Stop Tracking a File with git rm --cached
This is a different job: git rm --cached removes a file from the index entirely, so the next commit deletes it from the repository while your copy stays on disk. Use it for a file that should never have been committed, such as .env.
Warning: when another checkout pulls that commit, Git deletes the file there. On a server that deploys with git pull, that removes the live .env. Copy it aside on the server before you pull, and restore it afterward. The file also stays in the older commits, so change any password it held.
git rm --cached .env
echo ".env" >> .gitignore
git add .gitignore
git commit -m "Stop tracking .env"
Before the First Commit
In a new repository with no commits, git status shows "No commits yet", and git restore --staged restores from a last commit that does not exist, so it can stop with an error about HEAD. Use git rm --cached config.php instead, the form git status suggests at that stage; here it only unstages, because nothing has been committed yet, and the file stays on disk. git reset config.php also works.
Unstaging Is Not Discarding
Warning: the commands in this section throw away edits that were never committed, and Git cannot bring them back. Look at what you would lose first:
git diff config.php
If you are sure, discard the changes to that one file:
git restore config.php
| Command | Staging area | Your edits |
|---|---|---|
git restore --staged f | Cleared | Kept |
git reset | Cleared | Kept |
git restore f | Unchanged | Lost |
git reset --hard | Cleared | Lost |
If you are not sure, set the changes aside with git stash instead; git stash pop brings them back. For configuration files that matter, a copy outside the repository is a better safety net; see snapshot vs backup.
Undo a Commit but Keep It Staged
git reset --soft HEAD~1
If you already committed the file, this moves the branch back one commit and leaves that commit's changes staged; then unstage what you need as shown above. Do it only for a commit you have not pushed, because rewriting a pushed commit affects everyone who pulled it. The everyday commands around this are in basic Git commands with examples. To save typing, add a Git alias with git config --global alias.unstage "restore --staged", or a shell alias as shown in what .bashrc is and how to edit it.