Create a .gitignore file
Write and share Git ignore rules, check which patterns match, and keep exceptions working without accidentally treating inline comments as patterns.
Create a plain-text file named .gitignore in the repository root, with one pattern per line. Git applies the rules to untracked files as soon as the file is saved. Commit it when the rules are part of the project's shared setup.
Create the file
On Linux or macOS:
touch .gitignore
In Windows Command Prompt, create it only if it is absent:
if not exist .gitignore type nul > .gitignore
An editor can also save the file under that exact name. Check that it has not silently added a .txt extension.
Write patterns with comments on separate lines
# Log files at any level
*.log
# Directories named build at any level
build/
# target at the repository root only
/target
# Direct child text files in doc
doc/*.txt
# PDF files anywhere beneath doc
doc/**/*.pdf
# Keep this exception to the log rule
!important.log
# A name beginning with temp followed by one character
temp?
A line starting with # is a comment. Appending # explanation to a pattern does not create an inline comment; it changes what the pattern matches. A leading ! reverses a prior match, but Git cannot re-include a file when its parent directory is excluded entirely.
Start from a template when useful
GitHub can add a language-specific ignore template when you create a repository. You can also use the maintained GitHub gitignore templates, an editor extension, or a generator such as gitignore.io. Review generated rules before adopting them: project-specific files may need to remain tracked. Merge them into an existing file rather than overwriting it blindly with downloaded output.
Check the result
git check-ignore -v some-file.log
git status --ignored
The verbose check identifies the matching rule. It can also print a negated pattern, so read the pattern rather than interpreting any output as proof that the file is ignored. For an already tracked file, add --no-index when diagnosing the pattern itself.
FAQ
Why does a matching file still appear as modified?
It is already tracked. If the team intends to stop tracking it, git rm --cached -- file stages its removal from the repository while keeping your local copy. Commit that change with the ignore rule.
Should .gitignore itself be committed?
Yes, for shared rules. Put personal exceptions in .git/info/exclude or your global ignore file instead.
Which rule wins?
Within a matching precedence level, the last applicable pattern wins. A nested .gitignore can override one in an ancestor directory. Repository ignore files outrank info/exclude and the global ignore file; excluded parent directories still limit re-inclusion.
See gitignore, git-check-ignore, and git-rm.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan