- Global: create a .gitignore file in your home directory
- Per project: create a .gitignore file in the project's root source directory
- Per project: create a .git/info/exclude file in the project's hidden git directory
UpdateWell... okay, so really, there are 4 ways, but the 4th way I haven't found a good use case for.
Amazingly, this flexibility in design does have a purpose (I don't believe the original intention was for the developer to close his eyes and pick a gitignore to stick files in randomly). The granularity offered in this design allows you to have a clear separation of what is generated by the build, and what is ignored by personal preference. For example, you can commit a .gitignore that only ignores logs and test output, while using a global gitignore file to ignore your vim or emacs swap files.
If you don't pollute your project space with files or data that may not be useful to everybody, it's easier for new developers to jump onto your project and figure out what's going on without wading through the fluff. For example, here's a good reason for not checking in your .classpath or .project files in Eclipse. Going one step further, it's probably not even a good idea to commit a gitignore file that names them.
On a project with enough people, it's simply not feasible to commit a .gitignore that names swap files for every editor, project files for every IDE, DS_Store for people who only use macs, thumb.db files for people who only use windows, and so on. For smaller projects, it doesn't hurt anything, but there are other places where this information could possibly be stored. Here is what I propose for each method of ignoring files:
- Global .gitignore should ignore your favorite editor's swap files, the .DS_Store on a mac, or thumbs.db on windows; basically anything you personally need ignored for every project.
- Per project .gitignore should ignore only files generated by the build (logs, test output, build targets).
- Per project .git/info/exclude should ignore files you personally need, but only for this project. For example, if you are normally an Eclipse user, your global .gitignore should contain the .classpath and .project files, but for a particular project where you must use Netbeans, consider placing the nbproject directory in this file.