Ignoring Log Files in Git Repositories
Properly ignore log files in git repositories. Covers application logs, npm/yarn debug logs, error logs, and rotating log file patterns for all stacks.
Detailed Explanation
Log files are generated at runtime and should never be version-controlled. They contain timestamps, machine-specific paths, and transient state that changes with every execution. Committing them pollutes your git history with meaningless noise.
Common log patterns to ignore:
*.log
logs/
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
Package manager debug logs:
npm-debug.log— Created whennpm installornpm runencounters an error. Contains the full npm execution trace with environment details.yarn-debug.logandyarn-error.log— Yarn's equivalent debug output.pnpm-debug.log— pnpm's diagnostic log.
These files often contain your file system paths, environment variables, and system information that you may not want in a public repository.
Application logs:
*.log— The wildcard pattern catches most log files. Be cautious: if your project contains intentional.logfiles that are source material (rare but possible), use more specific patterns instead.logs/,log/— Common directories where applications write rotating log files.*.log.gz,*.log.[0-9]— Rotated and compressed log archives from tools likelogrotateorwinston.
Runtime and crash logs:
hs_err_pid*.log— JVM HotSpot crash dumps containing stack traces and memory state.crash.log— Generic crash reports from various runtimes.*.pid— Process ID files written by daemons and servers.
Best practices for log management:
- Configure your application to write logs to a dedicated
logs/directory and add that directory to.gitignore. - Use log aggregation services (Datadog, CloudWatch, ELK Stack) instead of relying on local log files for production debugging.
- Never log secrets — sanitize environment variables and credentials from log output at the application level.
- Git hooks — consider a pre-commit hook that warns if
.logfiles are staged accidentally.
Edge case: If you need to commit an example log file for documentation or testing, use the negation pattern !example.log to override the wildcard rule.
Use Case
A Node.js team keeps accidentally committing npm-debug.log files that expose internal file paths and environment details to their open-source repository.