DEV Community

Cover image for How to write .gitignore correctly
Takuzen Toh
Takuzen Toh

Posted on • Updated on

How to write .gitignore correctly

I've been frustrating when I cannot successfully make git ignore huge files such as 'node_modules' or 'mysql' etc.

Today I totally figured out why sometimes it doesn't work as I wish.

The biggest reason is that I have no idea if I want to ignore certain file, I DO NOT need to write the relative path to it, JUST have to write the name of the file or the folder, without any slash.

As long as you don't have one file with the same name in the same project folder and futhermore you don't want to ignore that one.

The reason why is because only writing the name of the file or the folder would make git ignore all the files with that name.





Enter fullscreen mode Exit fullscreen mode

every work folder would be ignored

the work folder which is on the same directory with .gitignore would be ignored

same with #1

same with #2

I recommend only having one .gitignore and put it on the root directory.

(you can have two, and if so the one on the deeper layer would be executed first)

Personally I feel it's confusing.

One last thing,
once you added/committed something, you have to delete its cache so you can let git ignore it.
The command following.

$ git rm --cached <file or directory>
Enter fullscreen mode Exit fullscreen mode

Thank you for reading, happy coding:)

Top comments (2)

dcsan profile image

Otsukare san!
If you can explain NOT rules! Eg


Especially when you have a second ! ignore the behavior is weird

cicirello profile image
Vincent A. Cicirello

@dcsan I found this article a while back that does a good job explaining the not rules: