How to ignore files in Git
· Category: Git
Short answer
Create a .gitignore file in your repository root and list files or patterns to exclude from tracking.
Steps
- Create the file:
touch .gitignore
- Add patterns:
node_modules/
*.log
.env
dist/
- Ignore already tracked files (stop tracking but keep locally):
git rm --cached filename
Tips
- Use templates from GitHub for common languages and frameworks.
- Patterns with
/at the start are relative to the.gitignorelocation. !important.lognegates a pattern to re-include a file.
Common issues
- Files already committed before adding to
.gitignorewill continue to be tracked. - Global ignore files can be set with
git config --global core.excludesFile.