Ignoring files with Git

Published by marco on

The helpful page, Ignoring files (GitHub), taught me something I didn’t know: there’s a file you can use to ignore files in your local Git repository without changing anyone else’s repository.

Just to recap, here are the ways to ignore a file:

  • Global .gitignore: you can designate basic exclusion directives that apply to all repositories on your system. This file is not committed to any repository or shared with others. Execute git config –global core.excludesfile ~/.gitignore_global to set the file to ~/.gitignore_global (for example). See the linked article for sample directives.
  • Per-repository global exclusions: add directives to the .git/info/exclude file in any repository. These directives are combined with any system-global directives to form the base exclusions for that repository. This file is not committed with the repository. This is the one I’d never heard of before.
  • .gitignore: add a file with this name to any directory. The directives in that file are merged with those from the parent directory to define the patterns that are excluded in that directory and all child directories. This is definitely the most common way to exclude files.
  • Exclude versioned files: and, finally, if your repository has files that are changed but not committed (e.g. configuration files), you can ignore future changes to those files with a call to git update-index –assume-unchanged path/to/file.txt. While this can be useful for legacy projects, it’s best to structure new projects so developers don’t have to rely on easily forgotten tricks like this.