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

  1. Create the file:
touch .gitignore
  1. Add patterns:
node_modules/
*.log
.env
dist/
  1. 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 .gitignore location.
  • !important.log negates a pattern to re-include a file.

Common issues

  • Files already committed before adding to .gitignore will continue to be tracked.
  • Global ignore files can be set with git config --global core.excludesFile.