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!
Advertisements
Try Out Code2care Dev Tools:

Advertisements

Advertisements
Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and software developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include learnings, tutorials, trouble-shooting steps, video tutorials, code snippets, how-to, blogs, articles, etc.

🎉 We are celebrating the 10th years of Code2care! Thank you for all your support!

We strongly support Gender Equality & Diversity.