~/
hackweb.dev
Ignoring Files
Quiz
⌘K
...
~/
/tutorials
/git/gitignore/edit
~ Contribute
Suggest a correction or improvement. The author reviews it before it goes live.
Loading...
Comment
0 / 300
Typo
Grammar
Broken link
Clarify
Code
en/tutorials/git/14gitignore
Write
Preview
Diff
# Ignoring Files Use `.gitignore` to prevent Git from tracking files you don't want committed, like dependencies, build output, or secrets. ## Creating a .gitignore Create a `.gitignore` file in your repository root: ```gitignore # Dependencies node_modules/ # Build output dist/ build/ # Environment variables .env .env.local # Logs *.log # OS files .DS_Store Thumbs.db ``` Git reads this file and skips anything that matches these patterns. ## Pattern Syntax - `node_modules/` — ignores the directory - `*.log` — ignores all `.log` files - `!important.log` — negation: track this file even though `*.log` is ignored - `.env*` — glob: matches `.env`, `.env.local`, `.env.production` ## Directory vs File Patterns Patterns ending with `/` match directories only. Without the trailing slash, Git matches both files and directories with that name. ```gitignore logs/ # only matches directories named logs logs # matches both files and directories named logs ``` ## Global Gitignore Set a global ignore for files across all repos: ```bash git config --global core.excludesfile ~/.gitignore_global ``` Useful for OS-specific files (`.DS_Store`, `Thumbs.db`) without polluting each project. ## Removing Tracked Files If you added a file to `.gitignore` after it was already tracked, you need to untrack it manually: ```bash git rm --cached filename.txt ``` The file stays in your working directory but Git stops tracking it. ## Best Practices - Add `.gitignore` before your first commit - Commit your `.gitignore` so the team shares it - Use a template for your tech stack (e.g., GitHub's gitignore templates) - Check what's being tracked with `git status` regularly - Review `.gitignore` periodically as projects evolve ## Common Mistakes - Forgetting that `.gitignore` only prevents tracking new files — already tracked files must be untracked with `git rm --cached` - Using absolute paths instead of relative patterns - Putting sensitive files in `.gitignore` instead of keeping them out of the repo entirely - Ignoring `.gitignore` itself (which is valid, but rare) - Not adding `.env` early and accidentally committing secrets
No changes yet
Reset to original
Submit suggestion
cancel