97

What I did:

I think there were some weird configurations from the github gui that caused this issue and prevented me from being able to easily use git from command line or even git-bash.

I ended up just uninstalling github and git then reinstalling just git for windows. I now have everything running off the command line(except ssh which I run from git-bash). Much easier and more reliable that the github gui.

Thanks to mu 無 for taking the time to try to figure this out. I didn't end up using his answer, but if I hadn't needed to do a reinstall of git it would have been what I needed to do.


I am using the github gui on my local machine. I just noticed that a commit I was about to make was going to update all of my recently update node modules. I set up my .gitignore to ignore the entire node_modules/ directory.

I'm not sure what to do about this. All the file types I included in .gitignore were ignored. It's just the directories that it seems to ignore.

Here is my .gitignore file:

#################
## Sublime Text
#################

*.sublime-project
*.sublime-workspace

#################
## Images
#################

*.jpg
*.jpeg
*.png
*.gif
*.psd
*.ai

#################
## Windows detritus
#################

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac crap
.DS_Store

#################
## Directories
#################

dev/
cms/core/config/
node_modules/
hal
  • 4,845
  • 7
  • 34
  • 57
  • what is the output of `git status`? Add that to the question... – Anshul Goyal Apr 07 '14 at 22:57
  • 1
    I don't know anything about that – hal Apr 07 '14 at 22:58
  • Go to your terminal, and run `git status` in the repository. Whatever is the output, put them here. – Anshul Goyal Apr 07 '14 at 22:59
  • I am on a windows machine. So terminal git commands don't work. `'git' is not recognized as an internal or external command, operable program or batch file.` – hal Apr 07 '14 at 23:01
  • Can you check if any file within the node_modules folder is already tracked? You will need to run `git log node_modules` in it bash, or check it on github. – Anshul Goyal Apr 07 '14 at 23:07
  • Yes, the node_modules directory is already committed from the originial commit. But the .gitignore file was in place at the time and was set to ignore the `node_modules/` directory – hal Apr 07 '14 at 23:10
  • I tried ignoring a newly created directory containing a .txt file and it worked fine. – hal Apr 07 '14 at 23:11
  • Check edits to my answer, you need to untrack the directory. Most probably it was being tracked even before you added the `.gitignore` entry, hence it didn't get ignored in the first place. – Anshul Goyal Apr 07 '14 at 23:13
  • 1
    Look this: [making git forget about a file that was tracked but is now in gitignore | StackOverflow](http://stackoverflow.com/questions/1274057/making-git-forget-about-a-file-that-was-tracked-but-is-now-in-gitignore/1274447#1274447) – bocai Aug 14 '16 at 13:55
  • @HalCarleton I'd recommend Git Bash. You can also use it as terminal in some IDEs (VSCode for sure). You can then use the early-on suggestions. – Neil Gaetano Lindberg Aug 06 '19 at 19:14

11 Answers11

324

Since the node_modules directory is already tracked as part of the repository, the .gitignore rule will not apply to it.

You need to untrack the directory from git using

git rm -r --cached node_modules
git commit -m "removing node_modules"

You can run the above 2 in git-bash.

After this, the .gitignore rule will ignore the directory away.

Note that this will remove the directory node_modules from your other repos once you pull the changes in. Only the original repo where you made that commit will still have the node_modules folder there.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • 1
    That is not a typo. The directory created by node is `node_modules/` – hal Apr 07 '14 at 23:02
  • the ignore rule is `node_modules`, and in your question, at the beginning it is `node_module`. – Anshul Goyal Apr 07 '14 at 23:04
  • How do I untrack without command line? Or how do I access git in command line on a windows machine and no bash? – hal Apr 07 '14 at 23:15
  • go to `git-bash`. Git for windows comes with its own `git-bash`. How do you otherwise commit from windows? – Anshul Goyal Apr 07 '14 at 23:16
  • I commit with the github gui. That's all I've ever used in windows. On linux I can just access git from the command line. – hal Apr 07 '14 at 23:17
  • Then do it on linux :). Note however this will remove the directory `node_modules` from your other repos once you pull the changes in – Anshul Goyal Apr 07 '14 at 23:18
  • I'm working on this repo on a windows machine where I need the directory. I found git-bash, but don't have access to this repo – hal Apr 07 '14 at 23:22
  • I don't think github gui allows for commands like `git rm` to be run. Btw what do you mean by you don't have access? you can `cd` to the repository path, and then run the commands afaik. – Anshul Goyal Apr 07 '14 at 23:30
  • I can't cd to the path. I have a directory with all my working repos in it. It will only `ls` one of the repos and it is not the one I'm trying to fix – hal Apr 07 '14 at 23:32
  • I used `ls` inside the containing directory and it only listed one sub-directory when there is actually several. But I'll play around with it. Or maybe just SSH into the live server that this deploys to and do it there. – hal Apr 07 '14 at 23:38
  • Cool then. Do remember that it will remove the `node_modules` from all other cones/forks, except the one on which you run this command. – Anshul Goyal Apr 07 '14 at 23:39
  • `git rm -r --cached node_modules` remove it. but next time when i use `git add .` it add the folder again. What the hack is this? – Max Feb 24 '16 at 12:24
  • @Max Did you add it to the `.gitignore`? – Anshul Goyal Feb 24 '16 at 13:58
  • Yes i have, it works for other folder. i fix it by adding directry name in .git\info\exclude file – Max Feb 24 '16 at 15:29
  • I had the same issue with my `node_modules` not been ignored. I think it is because I had generated the folder before adding my `.gitignore` file. What I did to fix it without using the command above is to delete the `node_modules` folder and regenerate it. Some how, when it meets the `.gitignore` file already present, the ignore rule will apply. – Eyong Kevin Enowanyo Feb 20 '21 at 05:31
  • Using the command `git rm -r --cached node_modules` had not effect, it worked after I forced the command with -f. – Romildo da Silva Jun 06 '23 at 14:58
34

Similar to Zach, I also used echo "node_modules/" >> .gitignore.

The problem was it had created the file with encoding UCS-2 LE BOM. Using notepad++ I changed the encoding to UTF-8 and voila - node_modules is now ignored.

enter image description here

Scotty.NET
  • 12,533
  • 4
  • 42
  • 51
21

If the files are already tracked the .gitignore file will not override this. You will need to use git rm --cached <files>

See the full details on git rm at https://www.kernel.org/pub/software/scm/git/docs/git-rm.html I ran into this once or twice myself early on with git and it was not quite what I expected either.

Matthew M
  • 1,155
  • 8
  • 9
  • 2
    This is more comprehensive answer to the title in question if we ignore the typo problems in the OP's question. Useful for people arriving w/ search. – Priya Ranjan Singh Apr 27 '16 at 11:54
5

If you work with node projects, I like this .gitignore:

# See http://help.github.com/ignore-files/ for more about ignoring files.

# dependencies
node_modules

# testing
coverage

# production
build

# misc
.DS_Store
.env
npm-debug.log
prosti
  • 42,291
  • 14
  • 186
  • 151
4

I had this problem. Somehow when I generated the file (using echo "node_modules" > .gitignore) it had inserted a garbage character at the beginning of the file (I blame powershell).

So, if you run into this problem try deleting your .gitignore and starting over.

Zach Dahl
  • 609
  • 5
  • 12
1

None of the solutions above worked for me and I don't know why

Eventually what I did was:

  1. Duplicated to project folder in order to don't work with the main one
  2. Then I removed .git & .gitignore file (CMD+SHIFT+DOT shows hidden files)
  3. Added .gitignore file with touch .gitignore at root and added node_modules
  4. Initialized git again with git init
  5. Added remote again with git remote add origin your_repo
  6. git add .
  7. git commit -m 'added ignore file'
  8. git push -u origin master

And continue working with this new directory. Please note that it was a solution for me because it was my first commit.

Sasan Soroush
  • 857
  • 8
  • 16
1

Well what works for me was add "node_modules" to .gitignore

backend\node_modules
frontend\your_proyect\node_modules
node_modules
Eli
  • 27
  • 2
1

Just remove the initial git configuration from your folder by rm -rf .git. Use the created .gitignore file and add all the file you want to add. Then reinitialize the git configuration in your folder with git init. This will solve your problem.

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

try this command, it will resolve your issue:

git config core.ignorecase true
Sudheer Singh
  • 555
  • 5
  • 10
0

I haven't seen this posted anywhere, as perhaps it should be obvious, until I forgot, but adding a comment to the end of a gitignore line is NOT actually a comment! Comments on their own line!

# .gitignore (this is a comment)
my_bad_dir/ # This is not a comment, and my_bad_dir/ won't be ignored
~$ man gitignore | grep comment
       •   A line starting with # serves as a comment. 
JWCS
  • 1,120
  • 1
  • 7
  • 17
0

If the node_module not ignoring after putting that in .gitignore folder then kindly check below steps and try. it work for me too.

  1. Create a .gitignore file with this entry.

    node_modules

  2. Remove the node_modules folder from the Git index using the git rm cached command.

    git rm -r --cached node_modules

  3. Commit the changes.

    git add . (if did not added before)

    git commit -am "removed node_modules folder"

  4. Push the changes to the remote.

    git push origin master

Avinash Raut
  • 1,872
  • 20
  • 26