DEV Community

Cover image for Git Lesson: How to Use .gitignore and .gitkeep?

Git Lesson: How to Use .gitignore and .gitkeep?

Rita {FlyNerd} Lyczywek on January 16, 2024

This article is translation of the original post from my blog: 🇵🇱 Git: Jak używać .gitignore i .gitkeep? If you work with Git and have your repo...
Collapse
 
kurealnum profile image
Oscar

Awesome article! It's worth noting that if you create a file, then decide that you want to .gitignore that file, you can use git rm --cached myawesomefile.txt.

This removes the file from the cache, and assuming that your .gitignore is correct, git will then not allow this file back into the cache.

Collapse
 
moopet profile image
Ben Sinclair

I've never liked using .gitkeep when there are ways of using .gitignore to do the same thing without requiring a hack. Usually, though, my approach is to not store empty directories at all!

Collapse
 
karlkras profile image
Karl Krasnowsky

so you know of a way using .gitignore that will allow you to track empty directories? Please tell.

Collapse
 
moopet profile image
Ben Sinclair

Yes, instead of naming your empty file, ".gitkeep", name it ".gitignore" :)

If you want to ensure the directory stays empty, put a * in it (and possibly ! .gitignore, I can't remember off the top of my head).

Using ".gitkeep" means you save an unrelated file to your repository and does not prevent you from accidentally adding whatever generated files end up under that directory in your local working version.

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️

This sentence reads like you probably changed it a bunch while editing and left an extra word in by accident :D

Collapse
 
ritaly profile image
Rita {FlyNerd} Lyczywek

thanks! just some translation oversight :D fixed!

Collapse
 
ahmadswalih profile image
Ahmad Swalih

Cool, I knew about .gitignore, but I didn't know about .gitkeep until now.

Collapse
 
ricardogesteves profile image
Ricardo Esteves

Great! Nice article @ritaly .

Collapse
 
morphzg profile image
MorphZG

Thanks for sharing. That is high quality content.

Collapse
 
jnareb profile image
Jakub Narębski

You can use empty .gitignore file instead of non-standard .gitkeep.

Collapse
 
ritaly profile image
Rita {FlyNerd} Lyczywek

yes, I mention that above, that we can track an empty folder through gitignore - it also works if you prefer that way

Collapse
 
blackgirlbytes profile image
Rizèl Scarlett

I didn't know about gitkeep. Thank you!

Collapse
 
dipayansukul profile image
Dipayan Sukul

Nice article elaborating the details

Collapse
 
matin192hp profile image
matin HP

Interesting :)

Some comments have been hidden by the post's author - find out more