I'm new to Git. A couple of days ago I tried to undo a "git add --all" with "git reset" after a quick Google suggested that. Luckily it appears no material harm was inflicted on my working directory, however Git has added a folder of angularjs modules I had listed in .gitignore, and now this folder (and everything in it) is showing up as "untracked" in "git status". It didn't prior to the reset, and nothing else in gitignore is doing this. I've tried a few solutions including "git rm -r --cached node_modules" and this Git untracked files list is wrong but it's still there. All suggestions appreciated!
6 Answers
The ".gitignore first line" bug has been reported before in ".gitignore
not working for me":
What really got gitignore working full for me was adding a comment on the first line of the file. Git wasn't parsing the exclude situated on the first line.
However, this shouldn't be a bug: the first line should work too.
What can change is:
- the encoding of your file
- a different eol (CRLF instead of LF) on the first line.
-
All I know is the unchanged gitignore had been working until the "git reset", then the one line at the top ("node_modules") apparently stopped working, until I added a new line above it. The editor is set to LF (but -- I did not edit the gitignore file prior to this problem materializing). – David Smith Oct 21 '13 at 16:35
-
@DavidSmith if the line at the top was to ignore a folder, it should have been `node_modules/`, with a final '`/`'. – VonC Oct 21 '13 at 16:45
-
I added the backslash but it still won't work without that extra top line. I have several other directories in gitignore following "node_modules" without the trailing backslash, and they all work. However I added the backslash to all as a precaution. – David Smith Oct 21 '13 at 17:23
-
@DavidSmith What git version are you using? – VonC Oct 21 '13 at 19:39
-
@DavidSmith It would be interesting to know if the issue persists with an up-to-date git 1.8.4.1, updated through an up-to-date apt repo: http://linuxg.net/how-to-install-git-1-8-4-on-ubuntu-13-10-13-04-12-10-12-04-linux-mint-16-15-14-13-elementary-os-0-2-and-pear-os-8/ – VonC Oct 22 '13 at 20:38
-
Go ahead and try it in a VM. I'm running an up-to-date Ubuntu server 12.04 64-bit, yo angular boilerplate. Lots of idiosyncracies in my set up may make the issue difficult to reproduce though. – David Smith Oct 23 '13 at 10:26
-
Try to update the git and also if you are using any git tools. After I updated my git version and git extension it went away. – Taher Ghulam Mohammed Mar 24 '20 at 05:24
-
@GhulamMohammedTaher Considering I wrote this answer more than 6 years ago, I am not surprised by your advise. – VonC Mar 24 '20 at 05:28
I resolved my similar issue with the answer accepted here: git still shows files as untracked despite .gitignore and rm -r --cached. What did I do wrong?.
It has to do with the encoding of the .gitignore file. Selecting plain "UTF-8" from Notepad++ resolved it for me.
I had a similar issue with an untracked folder persisting. I solved this by using the answer described here, I had to run:
git clean -f -d
To ensure directories were removed.

- 1,286
- 2
- 15
- 32
-
Note that this is a **destructive command** that physically deletes the files from the working tree (i.e. your hard drive), including any untracked files unrelated to the contents of .gitignore. – rrrrrrrrrrrrrrrr Nov 17 '21 at 19:28
After the command git rm -r --cached node_modules
did you do a commit to the repo?

- 46
- 2
-
No because I keep getting the same error (tried various iterations of folder name, with blackslash, and asterisk(s) etc. always the same:$ git rm -r --cached node_modules/ fatal: pathspec 'node_modules/' did not match any files – David Smith Oct 21 '13 at 13:56
-
This is because git doesn't index directories. To untrack the files you must untrack each file within the directory. – Impression Oct 21 '13 at 13:58
-
-
1OK I just tried a commit and the folder is still showing as "untracked" – David Smith Oct 21 '13 at 14:00
-
And did you tried `git rm -r --cached node_modules` without the slash (see your first comment on my answer) – Impression Oct 21 '13 at 14:03
-
Impression, yes I did, I also tried your suggestion of cd into the folder and still gets same error as above. It's like git doesn't recognize the folder, except to list it as "untracked". Doesn't the -r flag mean "recursive"? – David Smith Oct 21 '13 at 14:10
-
I think it means recursive. I'm sorry i don't know the answer right now. – Impression Oct 21 '13 at 14:17
-
2I solved it. Maybe I have a malformed gitignore, but I accidentally discovered that the first line is being ignored by gitignore (yeah, you read that right). When I added a new entry "nothing" to the top of git ignore, it stopped listing node_modules as"untracked". Whatever. – David Smith Oct 21 '13 at 14:20
-
That's strange. I've never heard or read that the first line is ignored by gitignore. – Impression Oct 22 '13 at 08:05
Maybe your .gitignore is incorrect. Try to edit the line about angularjs (maybe there is an extra /
at the beginning you have to remove, or add *
to ignore all)

- 6,732
- 3
- 36
- 49
(repeats my last comment under Impression's reply). I solved it. Maybe I have a malformed gitignore, but I accidentally discovered that the first line is being ignored by gitignore (yeah, you read that right). When I added a new entry "nothing" to the top of git ignore, it stopped listing node_modules as"untracked". Whatever.

- 89
- 1
- 6