Ignoring Files
10.1 What is .gitignore?
Often, there are files in your project that you do not want Git to track — logs, build outputs, temporary configs, environment variables, system files, etc.
Git provides the .gitignore file to help you control exactly what gets tracked and what doesn't.
A .gitignore file tells Git which files or directories should be completely ignored.
Git will:
• Not track them
• Not show them in git status
• Not include them in commits
Examples of files usually ignored:
• node_modules/
• *.log
• .env
• dist/
• Temporary IDE files like .vscode/, .idea/
Git provides the .gitignore file to help you control exactly what gets tracked and what doesn't.
A .gitignore file tells Git which files or directories should be completely ignored.
Git will:
• Not track them
• Not show them in git status
• Not include them in commits
Examples of files usually ignored:
• node_modules/
• *.log
• .env
• dist/
• Temporary IDE files like .vscode/, .idea/
10.2 Creating a .gitignore File
Simply create a file named .gitignore in the root of your repo:
Then add ignore patterns inside it, for example:
Each entry tells Git what to ignore.
1touch .gitignoreThen add ignore patterns inside it, for example:
1node_modules/
2.env
3*.log
4dist/Each entry tells Git what to ignore.
10.3 Pattern Rules in .gitignore
Ignore files by name
Ignore by extension
Ignore directories
Ignore files in specific folder
Ignore everything except specific files
Ignore all except one folder
Git's pattern system is powerful and flexible.
1secret.txtIgnore by extension
1*.log
2*.tmpIgnore directories
1node_modules/
2build/
3cache/Ignore files in specific folder
1logs/*.logIgnore everything except specific files
1*.config
2!important.configIgnore all except one folder
1*
2!/src/Git's pattern system is powerful and flexible.
10.4 Ignoring Files That Are Already Tracked
.gitignore only prevents new files from being tracked.
If a file is already committed, Git continues tracking it.
To stop tracking a file you've already committed:
Example:
Then add it to .gitignore.
If a file is already committed, Git continues tracking it.
To stop tracking a file you've already committed:
1git rm --cached <file>Example:
1git rm --cached .envThen add it to .gitignore.
10.5 Global .gitignore (User-Wide Ignore Rules)
You can create a global ignore file for patterns you want to avoid in all repositories on your machine.
Set a global ignore file:
Then edit that file:
This keeps OS-specific or editor-specific junk out of every project.
Set a global ignore file:
1git config --global core.excludesfile ~/.gitignore_globalThen edit that file:
1.DS_Store
2Thumbs.db
3*.swpThis keeps OS-specific or editor-specific junk out of every project.
10.6 Useful .gitignore Templates
GitHub maintains ready-made templates for various environments and languages (Node.js, Python, Java, Flutter, Go, Unity, etc.).
You can browse them by searching for: GitHub gitignore templates
Or use this command (if repo already has .gitignore):
Templates save time and reduce mistakes.
You can browse them by searching for: GitHub gitignore templates
Or use this command (if repo already has .gitignore):
1npx gitignore nodeTemplates save time and reduce mistakes.
10.7 Key Takeaways
• .gitignore tells Git which files or folders to ignore, preventing them from being tracked.
• It supports powerful patterns: extensions, names, directories, wildcard rules, and exceptions.
• To stop tracking an already-committed file, use git rm --cached.
• You can set up a global ignore file for system-wide patterns.
• Using standard ignore templates helps maintain clean repos and avoid accidental commits of sensitive or unnecessary files.
• It supports powerful patterns: extensions, names, directories, wildcard rules, and exceptions.
• To stop tracking an already-committed file, use git rm --cached.
• You can set up a global ignore file for system-wide patterns.
• Using standard ignore templates helps maintain clean repos and avoid accidental commits of sensitive or unnecessary files.