Donnerstag, 6. Dezember 2012

Advanced git: Different ways to exclude files

When I started using git, I very fast learned about the local .gitignore file. It's one of those handy features that everybody knows about and which is used in almost every project. In there you find excludes for build files, temporary files, caching files and so on.

Working with the local .gitignore can lead to a few problems though:
  • If you want to add specific files for each commiter (different IDEs or so) or OS (like the windows thumbs.db or Unix ~ backup files), your gitignore gets really messy 
  • If you add your IDE and then use another one later, you have to change gitignore files in all your projects
  • If you want to contribute to an OOS project, you are not supposed to change the gitignore, so you either not add those files to git and have clutter around or find another way.
And that's why there are different files which all can be used to ignore files within your git repository.


As said, this one is already known by almost everybody working with git. This file gets commited to your repository and should contain project specific excludes. As I am developing many symfony2 applications, most of my local gitignores containing the app/cache and app/logs directory, the composer.phar and stuff like this.

The local .gitignore is only used to ignore files in the current folder and down the folder path. It's not used in any other project and therefor has no side effects.


I believe this file is very unknown but is very helpful if you want to ignore certain files in one repository without changing the .gitignore file. As I am writing a bit on the symfony2-docs, I also build the docs with sphinx locally, leading to a _build folder containing all the html docs. Of course this files should not be commited.

Adding this files to the local .git/info/exclude file has the same effect as adding it to the .gitignore with the difference that this file lives inside the .git folder and is not commited to the repository. You can alter it without any effect on the repository. This is very handy for repositories you are contributing to!

Of course, if you delete the folder containing the repository and then clone it again, your .git/info/exclude is gone and you have to create it again.


For all those files you ignore in many different repositories (different binary, OS specific, temporary or IDE files), you can use a global gitignore. This one can contain stuff you never ever want to commit to any project like the files mentioned above.

Github has a nice help page about the global gitignore file: Ignoring Files

With all 3 files in place, I wish you happy "giting" :)

Keine Kommentare:

Kommentar veröffentlichen