A build may produce files we can recreate, while an editor may produce settings specific to one person. A .gitignore file describes paths Git should normally leave untracked. It is a plain text file, and the project can track it so others receive the same rules. Decide which generated artifacts and dependencies belong in history for the particular project; do not assume all generated files should be excluded.
In the example repository, write a root-level ignore file:
printf '# Generated output\n*.log\n/cache/\n*.tmp\n!/keep.tmp\n' > .gitignore
printf 'sample output\n' > build.log
printf 'keep this example\n' > keep.tmp
git status
git check-ignore -v build.log
git add .gitignore keep.tmp
git commit -m "Document ignored output"
Status normally omits untracked build.log, but shows .gitignore and keep.tmp. check-ignore -v identifies the rule and file responsible for ignoring the log. No secret is used in this example.
Read the patterns relative to the directory containing .gitignore:
#begins a comment.*.logmatches names ending in.logat any depth;*does not cross a slash./cache/matches the root-levelcachedirectory here, not a nested directory of that name. The trailing slash selects directories.*.tmpignores matching names, while the later!/keep.tmpmakes the root-levelkeep.tmpeligible again. Order matters among matching patterns at the same precedence.- A nested
.gitignoreapplies within its own directory and descendants. A deeper file can override a higher-level rule where Git can examine it. Git cannot re-include a file inside a directory excluded as a whole; keep the parent directory traversable and ignore its contents instead.
Ignore rules normally affect untracked paths, not files already in the index. To stop tracking a file while keeping its working copy, inspect the path and staged changes first, then use git rm --cached -- path with the actual path and commit the removal. This changes the next snapshot, not earlier commits. Do not confuse it with git rm without --cached, which also removes the working file.
Ignoring is not a security boundary: an explicit force-add can include ignored files, and prior commits may already contain them. Keep credentials out of the repository and inspect staged changes. If a secret has been committed or exposed, revoke or rotate it promptly and follow your incident procedure; adding a rule or removing the current file does not erase copies or history.
Next: Settings in context.