How to: Ignore files in Git (Part IV)

Ignorance is blissWith highly active projects, tracking irrelevant files can quickly become a pain in all of the asses that are working themselves off in the process.

I’m talking about those pesky little ever-changing files, machine or man generated, like log files, cache files, sql dumps, 3rd party project files for various assets, plain old text files some random co-worker keeps throwing in and so on.

Luckily, Git comes with a very useful feature for that too – it lets you ignore files and you can do it very easily in two slightly different ways:

On a project basis: Create a .gitignore file and put it in the root directory of the project e.g. /var/www/public_html/your-project.

Globally: Create a .gitignore_global file, put it in your home folder and add it to Git’s global config:

git config --global core.excludesfile ~/.gitignore_global

The contents of the file(s) can look something like this:

*.sql         # Ignore all .sql files
*.ini         # Ignore all .ini files
/tmp/*.*      # Ignore all files in /tmp
/cache/*.*    # Ignore all files in /cache
*.log         # Ignore all .log files
!change.log   # Don't ignore change.log

Note: Git will not ignore a file that was already tracked. To delete a file from staging index after it has been ignored, do git rm --cached <file>

Note: Git will not track empty directories, it just tracks files and the directories it takes to get to
them. In order to track empty directories, just put an empty file in them called .gitkeep e.g.
touch <empty_directory>/.gitkeep

You know it, I know it, ignorance is bliss, but keep it mind that too much of anything will get you nowhere (that’s the best case scenario). I think it’s about time we move on to more serious matters, like branching – it’s ok, adults do it all the time. Follow the next post in the series on How to: Use branches in Git (Part V).

One thought on “How to: Ignore files in Git (Part IV)

Leave a comment