More Git answers

by Danny
Published 2 min read

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.


_10
git restore --staged src/config.ts # unstage one file
_10
git 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 . or git add -A and 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.


_10
git 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
_10
git restore --staged debug.log
_10
git 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:


_10
git 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.


_10
git reset src/config.ts # unstage one file
_10
git 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:


_10
git rm --cached debug.log # remove from the index, keep the file on disk
_10
git 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:


_10
echo "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. Plain git restore file throws 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 --cached on 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. Use git restore --staged instead.
  • Expecting unstaging to undo a commit. If you have already committed, you need git reset --soft HEAD~1 or a similar command, not git restore --staged.
#

Was this resource helpful?