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:  Sample.java

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

So, to unstage the file Sample.java execute the below git command,

% git restore --staged Sample.java
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!

Top Hashtags:

Advertisements

Advertisements




Advertisements