Sub topic 13
Conventions and pitfalls
This chapter assumes you know the basic Git workflow (git init, git add, git commit, git status) and the .gitignore pattern syntax from earlier chapters. It collects the conventions that experienced teams follow and the mistakes that cause the most trouble.
What to ignore
A good rule: ignore anything that can be regenerated, anything specific to one machine, and anything secret. Commit everything else.
| Category | Examples | Where the rule belongs |
|---|---|---|
| Dependencies | `node_modules/`, `vendor/` | Project `.gitignore` |
| Build output | `/dist/`, `/build/` | Project `.gitignore` |
| Logs and caches | `*.log`, `.cache/` | Project `.gitignore` |
| Local environment files | `.env` | Project `.gitignore` |
| Editor and OS clutter | `.DS_Store`, `.idea/` | Global excludes file |
| Personal scratch files | `notes-to-self.txt` | `.git/info/exclude` |
Project rules are shared with everyone who clones the repository, so they should describe the project. Rules about your own editor or operating system belong in your global file, so each contributor does not have to maintain them in every project.
git config --global core.excludesFile ~/.gitignore_global
echo ".DS_Store" >> ~/.gitignore_globalCommit the .gitignore file
Add .gitignore to the repository like any other file. Then every clone applies the same rules. Keep it readable: group patterns under comment lines, as in this starter file.
# Dependencies
node_modules/
# Build output at the repository root only
/dist/
# Logs and local environment files
*.log
.envPitfall: ignoring a file does not untrack it
.gitignore only affects untracked files. If a file was already committed, adding a rule for it changes nothing. Remove it from the index and keep it on disk:
echo "config/local.json" >> .gitignore
git rm --cached config/local.json
git commit -m "Stop tracking config/local.json"Pitfall: secrets
Secrets are the most serious case of the previous pitfall. If you commit .env and then add it to .gitignore, the file is gone from future commits but stays in history, and anyone with the repository can read it.
A safer habit is to add .env to .gitignore before the first commit. Commit a template such as .env.example that lists variable names without real values. Keep that template out of the ignore rules by naming it so no pattern matches it, or re-include it with !.env.example.
Pitfall: negation under an ignored directory
Git never looks inside an ignored directory, so a ! rule for a file inside it has no effect.
# Does not work: Git never looks inside an ignored directory
logs/
!logs/keep.txtIgnore the contents instead of the directory:
logs/*
!logs/keep.txtPitfall: patterns that match more than you meant
An unanchored pattern matches at every depth. dist/ ignores a dist directory anywhere in the tree, including one inside a vendored package that you need. Use /dist/ when you mean only the top level. Broad wildcards such as *.json often swallow files like package.json. Prefer narrow patterns, and add a ! line when a specific file must stay.
Pitfall: trailing spaces and line endings
Trailing spaces in a pattern are ignored unless escaped with a backslash. A file saved with Windows line endings can occasionally produce patterns that do not match, so keep .gitignore in plain text with consistent line endings.
A debugging routine
When a file is ignored (or not) and you do not know why, work through these steps:
- Run
git check-ignore -v path/to/file. It prints the source file, line number, and pattern responsible. - Run
git status --ignoredto see everything Git is skipping. - Check whether the file is already tracked with
git ls-files path/to/file. If it prints a path, ignore rules do not apply. - If you really need an ignored file in the repository, stage it with
git add -f.
Exercise
You committed config/local.json last week. Today you add config/local.json to .gitignore, but git status still shows changes to it. Why, and what do you do?
Show answer
Ignore rules only apply to untracked files, and this file is already tracked. Run git rm --cached config/local.json and commit. The file stays on disk and is untracked from then on, so the rule takes effect. If the file contained secrets, also rotate them, because they remain in history.
Exercise
Where should a rule for .DS_Store go, and why?
Show answer
In a global excludes file set with core.excludesFile. The file comes from your operating system, not from the project, so it belongs in your personal configuration and does not need to appear in every repository's .gitignore.