omega
omega

Reputation: 43833

How to remove files from git staging area?

I made changes to some of my files in my local repo, and then I did git add -A which I think added too many files to the staging area. How can I delete all the files from the staging area?

After I do that, I'll just manually do git add "filename".

Upvotes: 1306

Views: 1607091

Answers (18)

Susobhan Das
Susobhan Das

Reputation: 1144

Below is the tree structure of a sample project.

AccountManageService/
|
|
Controller/SigninController.java
Security/WebSecurity.java
Service/
  |
  |
  AccountService.java
  SignupService.java
  1. To remove single file from staged
$ git restore --staged -- AccountManageService/Controller/SigninController.java
  1. Removing directory e.g. Service directory
$ git restore --staged -- AccountManageService/Service/

Upvotes: 1

Harry Slaughter
Harry Slaughter

Reputation: 459

None of these commands work for what I want to do. I want to uncommit changes that have not yet been pushed. I always thought that was 'unstaging', but none of the solutions above does this. Here's what does to this:

git reset --hard HEAD~1

Ship it.

Upvotes: -6

Nivethan
Nivethan

Reputation: 3589

MUCH SIMPLIFIED ANSWER (git v2.23 and above)

Below git command will remove all files from staging area

git restore --staged .

Or simply you can

git restore -S .

NOTE: Run these commands from your project root directory and don't forgot the . (dot) at the end!

Upvotes: 72

SandstormNick
SandstormNick

Reputation: 2011

Use the following to remove a specific file from the staging area:

git restore --staged <individual_file>

Or use the following to remove all the files that are currently staged:

git restore --staged .

In your git bash terminal after adding files to the staging area you can run a git status and the command is displayed for you above the current staged files:

$ git status 
On branch Releases/v1.1.1.x
Your branch is up to date with 'origin/Releases/v1.1.1.x'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified: Source Code/Server/Server.sln

Changes not staged for commit:
...

Upvotes: 141

majid asad
majid asad

Reputation: 409

Remove directory from staging area! git rm --cached <<repo/directory name>>

if this doesn't work use -f tag git rm --cached <<repo/directory name>> -f

Upvotes: 0

Ravindra Bhargava
Ravindra Bhargava

Reputation: 393

It is very simple:

  1. To check the current status of any file in the current dir, whether it is staged or not:

    git status

  2. Staging any files:

    git add . for all files in the current directory

    git add <filename> for specific file

  3. Unstaging the file:

    git restore --staged <filename>

Upvotes: 28

Vlad Bezden
Vlad Bezden

Reputation: 89499

If unwanted files were added to the staging area but not yet committed, then a simple reset will do the job:

$ git reset HEAD file
# Or everything
$ git reset HEAD .

To only remove unstaged changes in the current working directory, use:

git checkout -- .

Upvotes: 15

Ajith
Ajith

Reputation: 1535

use

git reset HEAD

This will remove all files from staging area

Upvotes: 5

Akash Bisariya
Akash Bisariya

Reputation: 4754

To remove all files from staging area use -
git reset
To remove specific file use -
git reset "File path"

Upvotes: 47

Amirouche
Amirouche

Reputation: 3756

Use "git reset HEAD <file>..." to unstage fils

ex : to unstage all files

git reset HEAD .

to unstage one file

git reset HEAD nameFile.txt

Upvotes: 4

norixxx
norixxx

Reputation: 3169

Now at v2.24.0 suggests

git restore --staged .

to unstage files.

Upvotes: 254

Antony Hatchkins
Antony Hatchkins

Reputation: 33974

Use

git reset

to unstage all the staged files.

Upvotes: 587

Amit Kaneria
Amit Kaneria

Reputation: 5808

You can reset the staging area in a few ways:

  1. Reset HEAD and add all necessary files to check-in again as below:

     git reset HEAD ---> removes all files from the staging area
     git add <files, that are required to be committed>
     git commit -m "<commit message>"
     git push 
    

Upvotes: 1

Bhaskar Dhariyal
Bhaskar Dhariyal

Reputation: 27

I tried all these method but none worked for me. I removed .git file using rm -rf .git form the local repository and then again did git init and git add and routine commands. It worked.

Upvotes: -9

Shad
Shad

Reputation: 1029

You could use

git reset HEAD

then add the specific files you want with

git add [directory/]filename

Upvotes: 38

Dan Rosenstark
Dan Rosenstark

Reputation: 69747

As noted in other answers, you should use git reset. This will undo the action of the git add -A.

Note: git reset is equivalent to git reset --mixed which does this

Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action. [ git reset ]

Upvotes: 11

Max
Max

Reputation: 22315

If you've already committed a bunch of unwanted files, you can unstage them and tell git to mark them as deleted (without actually deleting them) with

git rm --cached -r .

--cached tells it to remove the paths from staging and the index without removing the files themselves and -r operates on directories recursively. You can then git add any files that you want to keep tracking.

Upvotes: 178

Ash Wilson
Ash Wilson

Reputation: 24458

You can unstage files from the index using

git reset HEAD -- path/to/file

Just like git add, you can unstage files recursively by directory and so forth, so to unstage everything at once, run this from the root directory of your repository:

git reset HEAD -- .

Also, for future reference, the output of git status will tell you the commands you need to run to move files from one state to another.

Upvotes: 1493

Related Questions