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

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org



















Copyright Β© Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap