git command to remove/unstage files from staging area

If you have added files to the stage area using the git add command but you want to remove or upstage them, you can simply make use of the git restore command,

Intrestingly if you run the git status command and you have some files in the staging area, the command itself shows hints on how to restore files,

% git status

On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	new file:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

So, to unstage the file execute the below git command,

% git restore --staged
git command to remove unstage files from staging area

How to unstage all files?

Just add a . to restore all files that are staged,

% git restore --staged .

❗️Note: If you have a staged file and have made modifications to that file and you restore it, the local changes will be discarded i.e. lost, so be careful (unless you are using an IDE that maintains a history of a file like - Eclipse, VS Core or IntelliJ


Have Questions? Post them here!