Untracked files typically fall into two categories. They’re either files that have just been added to the project and haven’t been committed yet, or they’re files that we don’t want to be committed like compiled binaries.

While it’s definitely beneficial to include the former in the git status output, the latter can make it hard to see what’s actually going on in your repository.

Git lets you completely ignore files by placing rules in a special file called .gitignore. Any files or folder that you’d like to ignore should be included on a separate line, and the * symbol can be used as a wildcard.

Git sees every file in your working copy as one of three things:

  • tracked, file which has been previously staged or committed
  • untracked, file which has not been staged or committed
  • ignored, file which git has been explicitly told to ignore

Ignored files are usually machine generated files that can be derived from your repository source or should otherwise not be committed. for example dependencies, such as the contents of /node_modules or /packages, compiled code, such as .pyc, and .class files, build output directories, such as /bin, /out, or /target, files generated at runtime, such as .log or .lock.

The file .gitignore is checked in at the root of your repository. this file must be edited and committed by you when you have new files that you wish to ignore.


Git ignore patterns

.gitignore uses globbing patterns to match against file names

PatternExample
*.logdebug.log
logs/debug.log
**/logslogs/debug.log
logs/fum/foo.bar
!important/*.lognot important/debug.log
/debug.logdebug.log
not
logs/debug.log
debug.logdebug.log
logs/debug.log
debug?.logdebug0.log
debugg.log
not
debug10.log
debug[0-9].logdebug0.log
not
debug10.log
logs/**/debug.loglogs/debug.log
logs/monday/debug.log
  • * An asterisk is a wildcard that matches zero or more characters
  • ** A double asterisk match zero or more directories anywhere in the repository
  • ! Patterns defined after a negating pattern will re-ignore any previously negated files
  • / Prepending a slash matches files only in the “repository root”
  • ? A question mark matches exactly one character
  • [] Square brackets can also be used to match a single character from a specified range that can be numeric or alphabetic
  • # A hash to include comments
  • \ A backslash to escape pattern characters

Your repository can have multiple .gitignore files, but “repository root” will be the “directory” containing the .gitignore file