Coding

How to Unstage in Git: git restore --staged and git reset

To unstage in Git, run git restore --staged <file>. The file goes back to "Changes not staged for commit" and your edits stay in the working directory.

Replace <file> with a path such as src/app.py. The commands below are the ones Git's own documentation gives for this job. Commands that change files on disk are marked clearly, because unstaging itself should never touch your edits.

The short answer: unstage one file, a folder or everything

git restore --staged src/app.py
git restore --staged src/
git restore --staged .

The first line unstages one file, the second unstages everything under a folder, and the third unstages everything from the current directory down. Run git status afterwards. The unstaged paths now appear under "Changes not staged for commit" (or "Untracked files" if they were new), and nothing is left under "Changes to be committed" for those paths.

The git-restore documentation describes --staged as restoring "only the index", and it says that with --staged the contents are restored from HEAD. In plain terms: the staging area entry for that file is set back to what the last commit has, and the file in your folder is not read or written.

What unstaging actually does

Git keeps three copies of your project in play: the last commit (HEAD), the index (the staging area) and your working directory. git add copies the working directory version into the index. Unstaging is the reverse step for the index only: it copies the HEAD version back over the index entry. Your working directory version is never touched, so the edit is still there, just not queued for the next commit.

This is why unstaging is safe. The Pro Git book makes the same point about the older command: the file in your working directory is not touched, so it is relatively safe.

Example: unstage one of two staged files

Say you changed two files and meant to commit them separately, but staged both with git add *. Example output from git status, as shown in the Pro Git book:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   CONTRIBUTING.md
	renamed:    README.md -> README

Git prints the fix right under the heading. Unstage the one you want to commit later:

git restore --staged CONTRIBUTING.md

Example output from the next git status:

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	renamed:    README.md -> README

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   CONTRIBUTING.md

Now git commit records only the rename, and the edit to CONTRIBUTING.md is waiting for its own commit.

The older command: git reset

Before git restore existed, the standard answer was git reset HEAD <file>, and you will still see it in older tutorials and in the hint text of older Git versions. It still works. The git-reset documentation says that for a path, the command sets the staged version "to the version from the given commit or tree (which defaults to HEAD)", calls it "the opposite of git add <pathspec>", and states it is "equivalent to git restore --staged <pathspec>".

git reset HEAD src/app.py
git reset src/app.py
git reset

All three are valid. HEAD is the default, so the first two do the same thing. A bare git reset with no path unstages everything. Example output after unstaging with the older form:

Unstaged changes after reset:
M	CONTRIBUTING.md

The M means the file is modified in the working directory and is no longer staged.

git restore vs git reset: which one to use

GoalCommandTouches your files?
Unstage one filegit restore --staged <file>No
Unstage everythinggit restore --staged . or git resetNo
Unstage one hunk at a timegit restore --staged --patch <file>No
Discard unstaged edits to a filegit restore <file>Yes, edits are lost
Unstage and discard editsgit restore --source=HEAD --staged --worktree <file>Yes, edits are lost

For unstaging a path, pick git restore --staged: the name says what it does, and it is what current git status suggests. Keep git reset for moving the branch, as in how to undo the last commit.

Do not confuse unstaging with discarding

The one-flag difference between two commands decides whether your work survives. git restore --staged <file> only touches the index. git restore <file>, with no --staged, restores the working tree from the index, which overwrites your edits. The Pro Git book calls this "a dangerous command": any local changes to that file are gone, because Git replaced the file with the last staged or committed version. Its advice is to use it only when you are sure you do not want those changes.

The git-restore documentation also shows that giving both --staged and --worktree restores both places, which is the "unstage and throw away" row in the table. If you only meant to unstage, never add --worktree or -W.

Unstage a file in a repository with no commits yet

Right after git init there is no HEAD commit to restore from, and the documentation for git restore and git reset does not describe this case. For a newly added file there is a documented alternative: git rm --cached.

git rm --cached src/app.py

The git-rm documentation says --cached will "unstage and remove paths only from the index", and that working tree files "will be left alone". The file stays on disk as an untracked file. For a file that is already committed, do not use this to unstage an edit: it stages the file's removal from the repository, which is a different change. See git add -A vs git add . for how files end up staged by accident in the first place.

Unstage a staged deletion or rename

If you removed a file with git rm <file>, the deletion is staged and the file is gone from disk. git restore --staged <file> clears the staged deletion, but the file is still missing from your folder, so it shows as "deleted" under "Changes not staged for commit". Follow it with git restore <file> to bring the file back from the index. This is one of the cases where the second, file-writing command is what you want, and nothing is lost because the file's content is safe in the index.

If you already committed

Unstaging only applies before git commit. If the file is already in a commit, git restore --staged will not remove it from that commit. To fix the last commit, either undo it (see undo the last commit) or amend it, as described in changing a commit message, where unstaging before git commit --amend keeps the wrong files out of the new commit.

If you need to set staged work aside rather than unstage it, git stash saves both staged and unstaged changes and gives you a clean working tree.

Frequently asked questions

Does unstaging a file delete my changes?
No. git restore --staged <file> and git reset <file> only update the index, which is the staging area. The file in your working directory keeps every edit. The only commands in this area that throw edits away are the ones that also restore the working tree, such as git restore <file> without --staged.
What is the difference between git restore --staged and git reset?
For a path, they do the same thing: the git-reset documentation says git reset <pathspec> is equivalent to git restore --staged <pathspec>. git restore is the newer command with a clearer name, and recent git status output suggests it. git reset also has modes (--soft, --mixed, --hard) that move the branch, which git restore never does.
How do I unstage a file that was just deleted with git rm?
Run git restore --staged <file> to bring the entry back into the index, then git restore <file> to put the file back on disk. The first command alone clears the staged deletion but leaves the file missing from your working directory.
How do I unstage only some lines of a file?
Use patch mode: git restore --staged --patch <file>. Git walks through the staged hunks one at a time and asks whether to unstage each one. The git-restore documentation lists -p or --patch as a supported form of the command.

Sources