Techlife

Do You Know How .gitignore Works?

February 05, 2020

Git performs a great job of noticing when new files are added to the working directory or when current files that are in the repository are modified in our working directory, but that’s not always desirable.

Q: What if we don’t want to track those changes? What if it’s just a temporary file that we don’t care about or if it’s just a log file that’s constantly changing? 

Instead of having Git bother us about these files, we need a way to tell Git to ignore them and not notifying us when they change. If we create a file and put it in the root of our repository (the root of the project) called.gitignore, Git will read this file and use its rules when looking at the other files to make a decision on what should be tracked and what shouldn’t. That’s exactly what we’re looking for.

Changes made to git ignored files will be ignored by Git. So we need to understand a little bit about the kind of rules that we can put in that file. The simplest thing is just to put a simple name of the file and that file would get ignored, but we can also use pattern matching, basically simple regular expressions like an asterisk, question mark, character sets like:

  • * ? [aeiou] [0-9]

So for example if we want to ignore all files in the logs file directory that end in .txt, we can do that with logs/*.txt. You can also use negative expressions. For example we could say ignore all files that end in .php but do not ignore index.php. 

  • logs/*.txt
  • *.php
  • !index.php

If we want to ignore all files in a directory or folder, then we can use a trailing slash after the name so assets/videos/ would ignore all files that are in that directory. We can also put comments in the gitignore file by just putting a # sign in front of any text that we want it to be a comment in the gitignore file, maybe we want to provide the reason why we ignored something and any blank lines are simply going to be skipped. 

  • assets/videos/
  • # This is just a comment so it will be ignored

Let’s try an example so we get a feel for it. Inside our repository, our working directory is clean. Let’s add a file to it that’s going to be a temporary file. Let’s call it access.log. It might be used to log all of the access to the website.

It would be a constantly changing file. We would always have changes to it every time we came to Git. We don’t want to commit those changes to the repository. They’re not important for us to keep track of. So instead we just want to tell Git to ignore this file. 

In order to do that, we’re going to make another file in here and that file is going to be called .gitignore and inside that file we’re just simply going to put one line which is access.log. 

If we save it and type git status in the command line, it will show us a new gitignore file. It’s not mentioning anything about access.log anymore. Access.log is being ignored. 

We could add our gitignore file. It would then be part of our repository and anyone checking out our repository or working with it would also ignore the access.log. Before we commit that change, let’s come over and just add a few others to this. 

The first line is a comment. We know that because it has the # sign in front of it. Empty lines are going to be ignored so we can skip down a line. The 3rd and 4th lines are saying that anything that’s a compressed file (.gz & .zip). The fifth line says anything that it’s in the log directory. The sixth line indicates anything that ends in .log and also a number zero through nine, that happens sometimes if we have something rotating our log so we have log 1, log 2, log 3, and so on. 

Let’s imagine that we also had an assets directory and in there was another folder called videos. In the 7th line, we don’t want to keep those in our repository maybe because they’re such large files and it’s not really meant for us to track them. All files that are in that directory would be ignored except if we have assets/video/iphone_ with any number .mp4 in the 8th line. That’s going to give us a group of files (those will be kept).

It’s a list of rules that Git will use to know what to track and what not to track. Now our listed files are still there but Git is not going to bother us about it. It doesn’t matter if we make changes to it. Don’t forget to commit the .gitignore file into the repository.

By using .gitignore file and following its rule, we could deliberately tell Git not to track any file that we don’t need tracking. It’s a really simple and easy but powerful little feature that Git is providing us.

Other articles about Git you might like:

Also published on

Share post on

Get in touch

Simply register below to receive our weekly newsletters with the newest blog posts