1267

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
  • 3
    Hopefully you are looking for this: stackoverflow.com/questions/1505948/…
    – sumitb.mdi
    Commented Nov 1, 2013 at 16:07
  • 89
    git status already tells you exactly what to do if you want to unstage files.
    – Michael F
    Commented Nov 1, 2013 at 16:11
  • 21
    @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
    Commented 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
    Commented 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
    Commented Jan 22, 2023 at 15:16

17 Answers 17

1449

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
  • 9
    $ 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 Commented Feb 25, 2015 at 16:13
  • 13
    You can also use git reset @.
    – alex
    Commented Jun 9, 2016 at 9:18
  • 3
    @alex Is it better than just plain git reset? Commented Sep 10, 2016 at 17:56
  • 12
    @AntonyHatchkins @ is synonymous with HEAD.
    – alex
    Commented Sep 12, 2016 at 7:27
  • 4
    which is better git reset or git restore --staged Commented Mar 8, 2021 at 16:54
576

Use

git reset

to unstage all the staged files.

5
  • 10
    Can I ask how this differs to git reset HEAD -- or is it simply a more succinct way of performing the same operation?
    – ProNotion
    Commented Jan 17, 2018 at 10:52
  • 5
    @ProNotion Reset uses HEAD by default. What's the purpose of those -- in your git reset HEAD -- ? Commented 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
    Commented Jan 22, 2018 at 12:33
  • 37
    @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. Commented 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 Commented Mar 13, 2023 at 19:59
237

Now at v2.24.0 suggests

git restore --staged .

to unstage files.

4
  • 16
    this is the recommended method by git itself for unstaging files
    – Aris
    Commented Oct 20, 2021 at 9:43
  • That will result in a (no branch), so
    – Hu Xixi
    Commented Sep 5, 2022 at 11:45
  • 2
    Can anyone verify or explain the above note by Hu XiXi: "no branch"?
    – TonyG
    Commented Sep 7, 2022 at 19:57
  • 1
    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
    Commented Sep 7, 2022 at 20:03
175

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 -- ." Commented Nov 25, 2014 at 9:22
  • 1
    there's a fatal error about not removing files with "git rm -- ." Commented Nov 25, 2014 at 9:23
  • @AlexMills I update my answer to mention the actual options for rm to unstage all files.
    – Max
    Commented 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. Commented Jun 12, 2015 at 17:21
  • 2
    @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
    Commented Jun 13, 2015 at 2:40
138

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:
...
0
61

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!

5
  • 1
    git restore --staged . did nothing. All of the staged files are still staged. git version 2.40.1.windows.1
    – Ed Morton
    Commented Jul 17, 2023 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. git-scm.com/docs/git-restore
    – Nivethan
    Commented Jul 18, 2023 at 10:39
  • 1
    I ended up doing something different, I'm just saying that git restore --staged . did not unstage my files.
    – Ed Morton
    Commented Jul 18, 2023 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
    Commented Jul 19, 2023 at 4:36
  • Not sure what there is to check and I'm not having any other problems using git bash on Windows.
    – Ed Morton
    Commented Jul 19, 2023 at 11:27
45

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
    Commented Mar 3, 2021 at 14:13
38

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
    Commented 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
    Commented 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. Commented 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
    Commented Jan 30, 2018 at 17:13
  • 1
    Ok. Shortened and cleaned.
    – Shad
    Commented Feb 19, 2018 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>

14

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

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 Commented 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
  • 1
    What makes this answer different from the existing six answers? Commented 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

0

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.

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

3
  • 10
    Reading the git docs is generally more rewarding than following the advice from xkcd.com/1597 ;) Commented 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
    Commented Aug 14, 2018 at 15:18
  • Please don't do this, you will destroy your entire git repo. Commented Apr 2, 2019 at 23:58

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