A gitignore file specifies by choice untracked files that git should ignore.
Files already tracked by git don't seem to be affected; see the NOTES below for details.
Each line in a gitignore file identifies a pattern.
When deciding whether or not to ignore a path, git continually detects for gitignore patterns from multiple sources, with the subsequent order of precedence, from highest to lowest (within one level of precedence, the last matching pattern decides the outcome):
Patterns browse from the command line for those commands that support them.
Patterns read from a .gitignore file in a similar directory as the path, or in any parent directory, with patterns within the higher level files (up to the highest level of the work tree) being overridden by those files in the lower level down to the directory containing the file.
These patterns match relative to the path of the .gitignore file.