1093

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".

5
  • 2
    Hopefully you are looking for this: stackoverflow.com/questions/1505948/…
    – sumitb.mdi
    Nov 1, 2013 at 16:07
  • 81
    git status already tells you exactly what to do if you want to unstage files. Nov 1, 2013 at 16:11
  • 16
    @MichaelFoukarakis git status isn't as helpful if you want to unstage a whole directory if it floods the terminal with output (such as node_modules)
    – whiterook6
    Jul 28, 2014 at 21:42
  • 4
    In the future, instead of adding all, you may want to get comfortable with git add -p or git add --patch (they're the same). That flag allows you to interactively select which files or individual changes you want to stage -- you can then get a lot more finely grained with what work you include in the commit.
    – Gabe
    Jan 24, 2018 at 15:35
  • I've found it helpful to use a git status and then use the command xargs git add. I can than select the files in the status (left click of the mouse) and then paste it using the right click of the mouse or <kbd>Shift-Insert</kdb>. For a range of files, I use <kbd>Alt</kbd> and do a box selection using the mouse and then paste them as a group. At the end of each paste, I would press <kbd>Enter</kbd> and at the end of all pasting, I would press <kbd>Ctrl-D</kbd> to apply the files to the git add command.
    – Adrian
    Jan 22 at 15:16

16 Answers 16

1295

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.

8
  • 7
    $ git reset HEAD -- . produced fatal: Failed to resolve 'HEAD' as a valid ref. Note that I never did any commit; its the first git add after git init Feb 25, 2015 at 16:13
  • 10
    You can also use git reset @.
    – alex
    Jun 9, 2016 at 9:18
  • 2
    @alex Is it better than just plain git reset? Sep 10, 2016 at 17:56
  • 9
    @AntonyHatchkins @ is synonymous with HEAD.
    – alex
    Sep 12, 2016 at 7:27
  • 3
    which is better git reset or git restore --staged Mar 8, 2021 at 16:54
514

Use

git reset

to unstage all the staged files.

5
  • 8
    Can I ask how this differs to git reset HEAD -- or is it simply a more succinct way of performing the same operation?
    – ProNotion
    Jan 17, 2018 at 10:52
  • 4
    @ProNotion Reset uses HEAD by default. What's the purpose of those -- in your git reset HEAD -- ? Jan 18, 2018 at 9:36
  • 3
    @AntonyHatchkins it should have been followed by a full stop (period) and was taken from the example in the accepted answer.
    – ProNotion
    Jan 22, 2018 at 12:33
  • 29
    @ProNotion Sorry I misunderstood you. git reset HEAD -- . is different in that it only resets files in the current directory and below while git reset resets all the files in the project. Jan 23, 2018 at 10:07
  • 1
    actually I hop I had read this comment first, I kept trying solutions in previous comments but it was in vain, that command went quickly in one step @Antony Hatchkins Mar 13 at 19:59
169

Now at v2.24.0 suggests

git restore --staged .

to unstage files.

4
  • 7
    this is the recommended method by git itself for unstaging files
    – Aris
    Oct 20, 2021 at 9:43
  • That will result in a (no branch), so
    – Hu Xixi
    Sep 5, 2022 at 11:45
  • Can anyone verify or explain the above note by Hu XiXi: "no branch"?
    – TonyG
    Sep 7, 2022 at 19:57
  • Everyone here notes the "." argument as well as "<file>". That argument is just a common path reference. "./d1/d2" unstages all of the files in that path. This is great when you have a lot of changes for ./code or ./docs or ./configs that are unrelated, should be committed separately, and -oops- we were slow on commits and just did a single "add .".
    – TonyG
    Sep 7, 2022 at 20:03
160

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.

5
  • I suppose it is "git rm -- ." Nov 25, 2014 at 9:22
  • 1
    there's a fatal error about not removing files with "git rm -- ." Nov 25, 2014 at 9:23
  • @AlexMills I update my answer to mention the actual options for rm to unstage all files.
    – Max
    Nov 25, 2014 at 15:18
  • 3
    I tried this on a singular file like this: "git rm --cached my/file.java" and I see that file still in the staging area, but as deleted! @Max when you run this command are your files actually getting deleted or just un-staged? If you are not looking for that behavior I would go with answer. Jun 12, 2015 at 17:21
  • 1
    @OrwellHindenberg thanks for pointing that out! --cached is really to stop tracking files that you've already committed. So the file isn't actually deleted, but git thinks it is. I've clarified this in my answer.
    – Max
    Jun 13, 2015 at 2:40
109

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: enter image description here

0
39

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

1
  • 2
    Thank you for the time saving tip where I can remove just one of the files
    – Meenohara
    Mar 3, 2021 at 14:13
37

You could use

git reset HEAD

then add the specific files you want with

git add [directory/]filename
8
  • 1
    With excess folders, git status will tell you working directory clean - lies! git clean -df did ths job, thanks.
    – Leo
    May 30, 2016 at 1:26
  • 5
    Note that git clean -df will delete files permanently. On UNIX-like systems, it will call unlink() and your deleted files will not be recoverable.
    – Hanxue
    May 31, 2016 at 7:55
  • 13
    The OP asks about unstaging the files. You advise him to delete the files. I'd suggest you to read about what the staging area is prior to answering. Sep 10, 2016 at 17:59
  • 1
    The question was reedited some months AFTER I answered it. This can turn some answers invalid for the case.
    – Shad
    Jan 30, 2018 at 17:13
  • 1
    Ok. Shortened and cleaned.
    – Shad
    Feb 19, 2018 at 11:26
33

MUCH SIMPLIFIED ANSWER

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!

24

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>

13

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 -- .
10

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 ]

1
  • 2
    .. and to remove a single file from staging area, you can do git reset filenameToNotCommit Jan 23, 2018 at 0:09
5

use

git reset HEAD

This will remove all files from staging area

4

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
1

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 
    
1
  • What makes this answer different from the existing six answers? Sep 6, 2018 at 18:53
0

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

-10

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.

3
  • 8
    Reading the git docs is generally more rewarding than following the advice from xkcd.com/1597 ;) Mar 29, 2018 at 4:05
  • 1
    Maybe you had an integrity problem in your specific case and this was the option left, yet it's not recommended.
    – Shad
    Aug 14, 2018 at 15:18
  • Please don't do this, you will destroy your entire git repo. Apr 2, 2019 at 23:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.