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/

10.2 Creating a .gitignore File

Simply create a file named .gitignore in the root of your repo:
1touch .gitignore

Then 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
1secret.txt

Ignore by extension
1*.log 2*.tmp

Ignore directories
1node_modules/ 2build/ 3cache/

Ignore files in specific folder
1logs/*.log

Ignore everything except specific files
1*.config 2!important.config

Ignore 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:
1git rm --cached <file>

Example:
1git rm --cached .env

Then 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:
1git config --global core.excludesfile ~/.gitignore_global

Then edit that file:
1.DS_Store 2Thumbs.db 3*.swp

This 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):
1npx gitignore node

Templates 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.