How to undo git add
Unstaging never touches your working files. These commands only take files out of the next commit.
Unstage one file
git restore --staged path/to/file
# older git (before 2.23):
git reset HEAD path/to/file
Unstage everything (after a git add -A or git add .)
git restore --staged .
# or
git reset
Run git status afterwards: the files show as modified or untracked again, with your edits intact.
Brand-new repo with no commits yet
If git reset complains about HEAD, there is no commit to reset to. Use:
git rm -r --cached .
That removes everything from the index only; the files stay on disk.
You staged a .env or a key file
git restore --staged .env
echo ".env" >> .gitignore
If it was already committed but not pushed, undo the commit and keep the changes staged with git reset --soft HEAD~1, then unstage the file as above. If it was pushed, see I committed my .env to GitHub.
Stop needing this
KatchPath checks every git add and commit for .env files, key-shaped strings and runaway node_modules adds before they are staged:
curl -fsSL https://katchpath.com/install.sh | sh
katchpath install