.gitignore is not ignoring directories

82,507

Solution 1

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.

Solution 2

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

Solution 3

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.

Solution 4

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

Solution 5

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.

Share:
82,507

Related videos on Youtube

hal
Author by

hal

Updated on July 15, 2022

Comments

  • hal
    hal almost 2 years

    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/
    
    • Anshul Goyal
      Anshul Goyal about 10 years
      what is the output of git status? Add that to the question...
    • hal
      hal about 10 years
      I don't know anything about that
    • Anshul Goyal
      Anshul Goyal about 10 years
      Go to your terminal, and run git status in the repository. Whatever is the output, put them here.
    • hal
      hal about 10 years
      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.
    • Anshul Goyal
      Anshul Goyal about 10 years
      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.
    • hal
      hal about 10 years
      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
      hal about 10 years
      I tried ignoring a newly created directory containing a .txt file and it worked fine.
    • Anshul Goyal
      Anshul Goyal about 10 years
      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.
    • bocai
      bocai almost 8 years
    • Neil Gaetano Lindberg
      Neil Gaetano Lindberg almost 5 years
      @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.
  • hal
    hal about 10 years
    That is not a typo. The directory created by node is node_modules/
  • Anshul Goyal
    Anshul Goyal about 10 years
    the ignore rule is node_modules, and in your question, at the beginning it is node_module.
  • hal
    hal about 10 years
    How do I untrack without command line? Or how do I access git in command line on a windows machine and no bash?
  • Anshul Goyal
    Anshul Goyal about 10 years
    go to git-bash. Git for windows comes with its own git-bash. How do you otherwise commit from windows?
  • hal
    hal about 10 years
    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.
  • Anshul Goyal
    Anshul Goyal about 10 years
    Then do it on linux :). Note however this will remove the directory node_modules from your other repos once you pull the changes in
  • hal
    hal about 10 years
    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
  • Anshul Goyal
    Anshul Goyal about 10 years
    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.
  • hal
    hal about 10 years
    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
    hal about 10 years
    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.
  • Anshul Goyal
    Anshul Goyal about 10 years
    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.
  • Max
    Max about 8 years
    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?
  • Anshul Goyal
    Anshul Goyal about 8 years
    @Max Did you add it to the .gitignore?
  • Max
    Max about 8 years
    Yes i have, it works for other folder. i fix it by adding directry name in .git\info\exclude file
  • Priya Ranjan Singh
    Priya Ranjan Singh about 8 years
    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.
  • be_green
    be_green over 5 years
    Thank you! This was really frustrating me.
  • Justas Sam
    Justas Sam over 5 years
    Seems to be a powershell issue, since same thing happened here.
  • Kazimierz Jawor
    Kazimierz Jawor about 5 years
    This answer should have much more up votes as the problem and solution are really surprising. Thank you!
  • JD Byrnes
    JD Byrnes over 4 years
    I did exactly this, fml. Just deleted and re-created using VS Studio. Thanks @Scotty.NET
  • Eyong Kevin Enowanyo
    Eyong Kevin Enowanyo about 3 years
    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.