How do I remove a directory subtree from the staging area?

GitGitignoreGit Add

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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!

Solution 2 - Git

You can just use the command:

git reset

Solution 3 - Git

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

Solution 4 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAcornView Question on Stackoverflow
Solution 1 - GitGreg BaconView Answer on Stackoverflow
Solution 2 - GitMatthew FlaschenView Answer on Stackoverflow
Solution 3 - GitBusilinksView Answer on Stackoverflow
Solution 4 - GitVettisView Answer on Stackoverflow