86

I made a new repository, and ran git add -A. I then noticed that there was a folder containing about 100 files that shouldn't have been included, so I added it to .gitignore.

How do I now clear the staging area so that I can add all my files again taking into account the updated .gitignore?

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
Acorn
  • 49,061
  • 27
  • 133
  • 172
  • If you've NOT done your first commit yet, blow away the .git folder. Create the .gitignore file with the right contents. Start over. git init, stage, commit. – Gishu Mar 27 '13 at 07:51

4 Answers4

179

In #git, you said you unintentionally added a directory that should have been ignored, so run

git rm --cached -r directory-name

to recursively remove the tree rooted at directory-name from the index.

Don't forget to update .gitignore!

Greg Bacon
  • 134,834
  • 32
  • 188
  • 245
  • 1
    Would `-r .` be simpler and do the same thing? – Jonathan Allard Sep 16 '12 at 19:32
  • 4
    can you mention why the --cached is necessary? I don't see why it is – Alexander Mills May 30 '15 at 01:06
  • 5
    @AlexMills The question specified a directory that had been added using `git add` to the staging area, also known as the cache or index. According to the [`git rm` documentation](https://git-scm.com/docs/git-rm) on `--cached`: “Use this option to unstage and remove paths only from the index. Working tree files, whether modified or not, will be left alone.” – Greg Bacon Nov 10 '15 at 01:42
30

You can just use the command:

git reset
Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
4

Make sure you remember to put the s in --global core.excludesfile .gitignore.txt

excludesfile vs excludefile

Maybe this will save someone else the hour I lost...

Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
Busilinks
  • 87
  • 7
1

With a fairly modern git version you can now do:

git reset foldername/*

I found earliest documentation for this feature from version 2.12.5 onwards, but it might work on older version of git too.

Vettis
  • 313
  • 1
  • 2
  • 9