1156

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

Zoltan Toth
  • 46,981
  • 12
  • 120
  • 134
omega
  • 40,311
  • 81
  • 251
  • 474
  • 3
    Hopefully you are looking for this: http://stackoverflow.com/questions/1505948/how-do-i-remove-a-single-file-from-the-staging-area-of-git-but-not-remove-it-fro – sumitb.mdi Nov 01 '13 at 16:07
  • 86
    `git status` already tells you exactly what to do if you want to unstage files. – Michael Foukarakis Nov 01 '13 at 16:11
  • 18
    @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 '14 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 '18 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 Shift-Insert. For a range of files, I use Alt and do a box selection using the mouse and then paste them as a group. At the end of each paste, I would press Enter and at the end of all pasting, I would press Ctrl-D to apply the files to the `git add` command. – Adrian Jan 22 '23 at 15:16

16 Answers16

1350

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.

Kyle Kelley
  • 13,804
  • 8
  • 49
  • 78
Ash Wilson
  • 22,820
  • 3
  • 34
  • 45
536

Use

git reset

to unstage all the staged files.

Antony Hatchkins
  • 31,947
  • 10
  • 111
  • 111
  • 9
    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 '18 at 10:52
  • 4
    @ProNotion Reset uses `HEAD` by default. What's the purpose of those `--` in your `git reset HEAD --` ? – Antony Hatchkins Jan 18 '18 at 09: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 '18 at 12:33
  • 30
    @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. – Antony Hatchkins Jan 23 '18 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 – Yusuf al-Imrani Mar 13 '23 at 19:59
193

Now at v2.24.0 suggests

git restore --staged .

to unstage files.

norixxx
  • 2,549
  • 2
  • 19
  • 26
  • 8
    this is the recommended method by git itself for unstaging files – Aris Oct 20 '21 at 09:43
  • That will result in a (no branch), so – Hu Xixi Sep 05 '22 at 11:45
  • Can anyone verify or explain the above note by Hu XiXi: "no branch"? – TonyG Sep 07 '22 at 19:57
  • Everyone here notes the "." argument as well as "". 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 07 '22 at 20:03
166

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.

Max
  • 21,123
  • 5
  • 49
  • 71
  • I suppose it is "git rm -- ." – Alexander Mills Nov 25 '14 at 09:22
  • 1
    there's a fatal error about not removing files with "git rm -- ." – Alexander Mills Nov 25 '14 at 09:23
  • @AlexMills I update my answer to mention the actual options for `rm` to unstage all files. – Max Nov 25 '14 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. – OrwellHindenberg Jun 12 '15 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 '15 at 02:40
126

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:
...
Slate
  • 221
  • 5
  • 14
SandstormNick
  • 1,821
  • 2
  • 13
  • 24
44

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!

Nivethan
  • 2,339
  • 19
  • 22
  • `git restore --staged .` did nothing. All of the staged files are still staged. `git version 2.40.1.windows.1` – Ed Morton Jul 17 '23 at 17:10
  • @EdMorton The git restore command was introduced in Git version 2.23.0. The Git documentation indicates that there have been no changes to the git restore command from version 2.35.1 to 2.41.0 I recommend you to check the usage of this command on following link. https://git-scm.com/docs/git-restore – Nivethan Jul 18 '23 at 10:39
  • I ended up doing something different, I'm just saying that `git restore --staged .` did not unstage my files. – Ed Morton Jul 18 '23 at 10:42
  • @EdMorton i just checked again on my mac and linux(ubuntu) and i can confirm both of the above command works. so nothing wrong with the command. you need to check your windows environment. – Nivethan Jul 19 '23 at 04:36
  • Not sure what there is to check and I'm not having any other problems using `git bash` on Windows. – Ed Morton Jul 19 '23 at 11:27
41

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

Akash Bisariya
  • 3,855
  • 2
  • 30
  • 42
37

You could use

git reset HEAD

then add the specific files you want with

git add [directory/]filename
Shad
  • 1,021
  • 12
  • 21
  • You can add -x parameter and ignored files will be removed too. It's useful if you mess up with your .gitignore and have something useful there (as in my case). (https://git-scm.com/docs/git-clean) – Alex Pogiba Feb 11 '16 at 07:20
  • 1
    With excess folders, `git status` will tell you *working directory clean* - lies! `git clean -df` did ths job, thanks. – Leo May 30 '16 at 01: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 '16 at 07: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. – Antony Hatchkins Sep 10 '16 at 17:59
  • Thank you for deleting my entire unsaved project. – Vansuita Jr. Jan 30 '18 at 13:11
  • 1
    The question was reedited some months AFTER I answered it. This can turn some answers invalid for the case. – Shad Jan 30 '18 at 17:13
  • @PavelRyzhov replaced "in" with "from" and removed "Thanks". Neither the title nor the idea of the question wasn't changed. Your answer answers another question. As for the asked question, it doesn't add anything relevant to other answers. I'd suggest you make your answer more compact and more to the point or delete it as it proves to be destructive to some people. – Antony Hatchkins Feb 19 '18 at 03:25
  • 1
    Ok. Shortened and cleaned. – Shad Feb 19 '18 at 11:26
26

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>

Andrea Carraro
  • 9,731
  • 5
  • 33
  • 57
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 -- .
Vlad Bezden
  • 83,883
  • 25
  • 248
  • 179
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 ]

Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421
5

use

git reset HEAD

This will remove all files from staging area

Ajith
  • 1,447
  • 2
  • 17
  • 31
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
Amirouche Zeggagh
  • 3,428
  • 1
  • 25
  • 22
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 
    
Paul R
  • 208,748
  • 37
  • 389
  • 560
Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38
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

majid asad
  • 303
  • 5
  • 14
-9

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.