If you already know some basic git commands, it is time to learn about .gitignore.
When I started to learn Git, I read that .gitignore is basically a file that helps hide other files to keep a repository cleaner, meaning we are telling Git not to track every single file. Ok, so the concept is clear but my question back then was: in which context or what kind of files do I exactly want to hide?
I had no idea but understood it much better while doing the following exercise: how to start unit testing.
I read the article because I wanted to learn about unit tests (pieces of code that help check whether your code is working as expected or not) and while going through the tutorial and comparing my code with the author's solution, I realized some extra files like node_modules or .DS_store automatically showed up in my repository. I didn't really know how they got there and what they meant but, as every beginner, I thought: as my unit test is working, all good.
Fortunately, my dear mentor reviewed my code and gave me some useful suggestions:
use .gitignore to make those irrelevant files disappear and make your repository cleaner.
Some of the files .gitignore can hide are system files (like .DS_Store), dependency caches (like node_modules or packages) or files with sensitive data.
GitHub already offers the possibility to add .gitignore when creating a repository.
Gitignore is a text document that looks like this.
My mentor also showed me a very cool tool to create gitignore templates. Find the video tutorial here.
In my case, I did the unit-test exercise without having any .gitignore and I thought that I had to restart the whole exercise again. Fear not! There is also a way to create a .gitignore file and move all nonsense files into it. I used the following commands for it:
Thanks to this my unit test repository looks much better now. I hope this helps you understand better gitignore now and you can start applying it in your projects.
Top comments (0)