Staged a file by mistake? Use git restore --staged to take it out of the index without losing your edits, plus the older git reset form and what to do in a new repo.
The short answer
#Run git restore --staged with the file name. It takes the file out of the staging area (the index) and leaves your edits in the working tree exactly as they were. Nothing is lost; the file just goes back to being a normal unstaged change.
_10git restore --staged src/config.ts # unstage one file_10git restore --staged . # unstage everything
Why it happens
#Git has three places a change can live: your working tree (the files on disk), the index (what will go into the next commit) and the commit history. git add copies the current version of a file from the working tree into the index. It does not commit anything.
So "undoing" git add means making the index match the last commit again for that file, while leaving the working tree alone. That is exactly what git restore --staged does: it restores the index entry from HEAD.
The usual ways people end up here:
- Running
git add .orgit add -Aand catching files you did not mean to include, such as a local config file or a debug log. - Staging a file, then deciding the change belongs in a separate commit.
- Adding a large generated file and noticing before you commit.
If you run git status, Git even prints the command for you under "Changes to be committed".
How to fix it
#Use git restore --staged
This is the modern, clearest command, available since Git 2.23.
_10git status_10# Changes to be committed:_10# (use "git restore --staged <file>..." to unstage)_10# modified: src/config.ts_10# new file: debug.log_10_10git restore --staged debug.log_10git status_10# debug.log is now listed under "Untracked files" again
You can pass several paths or a directory. To unstage only some hunks of a file, add -p and Git will ask about each one:
_10git restore --staged -p src/config.ts
Use git reset on older Git versions
Before git restore existed, the standard way was git reset with a path. It still works and does the same thing for this case.
_10git reset src/config.ts # unstage one file_10git reset # unstage everything
With a path and no mode flag, git reset only touches the index. It does not move your branch and does not change files on disk.
In a brand new repository with no commits
Both commands above restore the index from HEAD. In a repository where you have not made the first commit yet, there is no HEAD, so use git rm --cached instead:
_10git rm --cached debug.log # remove from the index, keep the file on disk_10git rm -r --cached . # unstage everything in a new repo
Stop it happening again
If the file should never be committed, add it to .gitignore so git add . skips it in future:
_10echo "debug.log" >> .gitignore
It also helps to review before staging with git add -p, which shows each change and asks whether to include it.
Common pitfalls
#- Leaving out
--staged. Plaingit restore filethrows away your working tree changes to that file, which is a different and destructive operation. - Reaching for
git reset --hard. That unstages everything but also discards all uncommitted edits in tracked files. - Using
git rm --cachedon a file that is already committed. In an existing repository this stages the file's deletion, so your next commit removes it from the repository. Usegit restore --stagedinstead. - Expecting unstaging to undo a commit. If you have already committed, you need
git reset --soft HEAD~1or a similar command, notgit restore --staged.
Related reading
#Was this resource helpful?