# .gitignore not working

**URL:** <https://forum.codewithmosh.com/t/gitignore-not-working/3639>\
**Category:** Git\
**Created:** [March 28, 2021, 5:21pm UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639 "2021-03-28T17:21:12Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![aksu27](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@aksu27](https://forum.codewithmosh.com/u/aksu27)\
**Post date:** [March 28, 2021, 5:21pm UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/1 "2021-03-28T17:21:12Z")

</div>

Hello fellow students of Mosh  
I am currently taking the Node course for a project I am working on for my university. I am trying to use .gitignore to exclude node\_modules but it doesn’t seem to be working. First I thought that this might be due to the node\_modules folder being under another folder. But even when I moved the .gitignore file to the same folder as the node\_modules it’s still not working. Do you guys have any suggestions on how can I solve this?

---

<div class="post-metadata">

**Author:** ![AzharUddinSheikh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/azharuddinsheikh/32/5561_2.png) [@AzharUddinSheikh](https://forum.codewithmosh.com/u/AzharUddinSheikh)\
**Post date:** [March 29, 2021, 7:54am UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/2 "2021-03-29T07:54:36Z")

</div>

what command you are using to exclude may I know

try this in bash:

touch .gitignore (creating file)  
echo node\_modules\>\>.gitignore (this ll add your file into git ignore))

note \>\> if you already add this file (node\_modules) to staging area then this file ll not be ignored

---

<div class="post-metadata">

**Author:** ![gagglepod](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/gagglepod/32/508_2.png) [@gagglepod](https://forum.codewithmosh.com/u/gagglepod)\
**Post date:** [March 29, 2021, 2:33pm UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/3 "2021-03-29T14:33:04Z")

</div>

My .gitignore file sits a the root of my project folder.  
All that I have in it is:

_node\_modules/_  
_coverage/_

That works for me.

---

<div class="post-metadata">

**Author:** ![William](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@William](https://forum.codewithmosh.com/u/William)\
**Post date:** [March 30, 2021, 11:35am UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/4 "2021-03-30T11:35:54Z")

</div>

> [@AzharUddinSheikh](#):
>
> note \>\> if you already add this file (node\_modules) to staging area then this file ll not be ignored

This is indeed important.

---

<div class="post-metadata">

**Author:** ![aksu27](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@aksu27](https://forum.codewithmosh.com/u/aksu27)\
**Post date:** [April 2, 2021, 11:56am UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/5 "2021-04-02T11:56:45Z")

</div>

What does that exactly mean, the staging area?

---

<div class="post-metadata">

**Author:** ![aksu27](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@aksu27](https://forum.codewithmosh.com/u/aksu27)\
**Post date:** [April 2, 2021, 11:58am UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/6 "2021-04-02T11:58:44Z")

</div>

I am not using any commands over terminal. I am working on the project over VSC and that is how I basically created the .gitignore file.

---

<div class="post-metadata">

**Author:** ![William](https://avatars.discourse-cdn.com/v4/letter/w/b19c9b/32.png) [@William](https://forum.codewithmosh.com/u/William)\
**Post date:** [April 2, 2021, 12:16pm UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/7 "2021-04-02T12:16:30Z")

</div>

Creating .gitignore by VSC or from terminal is both okay.

As a test you could create a new project, add .gitignore file and than do the GIT commands. Still the same problem?

---

<div class="post-metadata">

**Author:** ![AzharUddinSheikh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/azharuddinsheikh/32/5561_2.png) [@AzharUddinSheikh](https://forum.codewithmosh.com/u/AzharUddinSheikh)\
**Post date:** [April 2, 2021, 3:35pm UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/8 "2021-04-02T15:35:38Z")

</div>

Git staging is an area where git allow to continue making changes to the working directory

When you do git add \* (it means you add all your file to staging area) if you want to ignore some file it is necessary to add file into gitignore before running this command (git add \*)

---

<div class="post-metadata">

**Author:** ![aksu27](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@aksu27](https://forum.codewithmosh.com/u/aksu27)\
**Post date:** [April 2, 2021, 3:47pm UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/9 "2021-04-02T15:47:53Z")

</div>

Well, before I do git add\*, the .gitignore file is already set including the files to be excluded.

---

<div class="post-metadata">

**Author:** ![aksu27](https://avatars.discourse-cdn.com/v4/letter/a/cdc98d/32.png) [@aksu27](https://forum.codewithmosh.com/u/aksu27)\
**Post date:** [April 2, 2021, 3:51pm UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/10 "2021-04-02T15:51:09Z")

</div>

So it’s basically trying to add both files, .gitignore and node\_modules, to the staging area at the same time. But I guess it is supposed exclude node\_modules in this case right?

---

<div class="post-metadata">

**Author:** ![AzharUddinSheikh](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/azharuddinsheikh/32/5561_2.png) [@AzharUddinSheikh](https://forum.codewithmosh.com/u/AzharUddinSheikh)\
**Post date:** [April 2, 2021, 4:41pm UTC](https://forum.codewithmosh.com/t/gitignore-not-working/3639/11 "2021-04-02T16:41:59Z")

</div>

I didn’t get you let say

I have 2 file in a root folder (file1.py and file2.py) but I want to ignore file2 and commit

Here what I do

> git init  
> touch .gitignore  
> echo file2.py \> .gitignore  
> git add \* (.gitignore and file1 ll be added)  
> git commit -am"initial commit"

Test

Lets edit file2

> git status  
> (You ll see a message nothing to commit )
