.gitignore
The .gitignore file defines patterns for files Git should normally leave untracked. Common examples include build output, dependency directories, local environment files, editor metadata, and logs.
Ignoring a file does not remove it from history if it is already tracked. In that case, you need to remove it from the index while optionally keeping it on disk.
Useful Ignore Rules
# .gitignore
node_modules/
dist/
.env
*.log
.vscode/
# Stop tracking an already committed environment file
git rm --cached .env💡 The patterns ignore common generated or local files.
git rm --cached removes a tracked file from Git's index while leaving the local file on disk.Key Takeaways
- Ignore rules affect untracked files.
- Adding a tracked file to
.gitignoredoes not automatically untrack it. - Never commit secrets such as production credentials.
Knowledge Check
What happens if a file is already tracked and you add it to `.gitignore`?