Git Notes
Learn how to use .gitignore files to exclude files and directories from Git tracking. Master patterns, global ignores, and best practices for different project types.
The .gitignore file tells Git which files and directories to exclude from tracking. This is essential for keeping your repository clean by ignoring build artifacts, dependency folders, environment files, and other generated content.
Why Use .gitignore?
Not every file in your project should be tracked by Git:
- Dependencies (
node_modules/,vendor/) — can be reinstalled - Build outputs (
dist/,build/) — can be regenerated - Environment files (
.env) — contain secrets - OS files (
.DS_Store,Thumbs.db) — system-specific - IDE files (
.idea/,.vscode/) — editor-specific settings - Log files (
*.log) — temporary runtime data
Creating a .gitignore File
Create a .gitignore file in your project root:
touch .gitignoreAdd patterns to ignore:
# Dependencies
node_modules/
vendor/
# Build output
dist/
build/
*.min.js
*.min.css
# Environment files
.env
.env.local
.env.production
# OS files
.DS_Store
Thumbs.db
# IDE files
.idea/
.vscode/
*.swp
*.swo
# Log files
*.log
npm-debug.log*Gitignore Pattern Syntax
Basic Patterns
| Pattern | What It Matches |
|---|---|
file.txt | File named file.txt in any directory |
/file.txt | File named file.txt only in root |
dir/ | Directory named dir and all its contents |
*.log | All files ending in .log |
*.py[cod] | Files ending in .pyc, .pyo, or .pyd |
doc/*.txt | .txt files in doc/ (not subdirectories) |
doc/**/*.txt | .txt files in doc/ and all subdirectories |
Wildcards and Special Characters
Directory vs File Patterns
# Trailing slash means directory only
build/ # Ignore the build directory
logs/ # Ignore the logs directory
# No trailing slash matches both files and directories
build # Ignores file or directory named "build"Step-by-Step Example
# Create a new project
mkdir my-project && cd my-project
git init
# Create some files
touch index.js app.css .env secret.key
mkdir node_modules logs
touch node_modules/package.js logs/error.log
# Check status - everything is untracked
git statusOn branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.env
app.css
index.js
logs/
node_modules/
secret.key# Create .gitignore
cat > .gitignore << EOF
node_modules/
logs/
.env
*.key
EOF
# Check status again
git statusOn branch main
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
.gitignore
app.css
index.js
nothing added to commit but untracked files presentThe ignored files no longer appear in git status.
Global Gitignore
Set up a global gitignore for patterns that apply to all your projects:
# Create a global gitignore file
touch ~/.gitignore_global
# Configure Git to use it
git config --global core.excludesFile ~/.gitignore_globalAdd common patterns to ~/.gitignore_global:
# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
Thumbs.db
ehthumbs.db
Desktop.ini
# Editor files
*.swp
*.swo
*~
.idea/
.vscode/settings.json
*.sublime-workspaceCommon .gitignore Templates
Node.js Project
node_modules/
dist/
build/
.env
.env.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
.npm
.yarn-integrity
coverage/Python Project
Java Project
*.class
*.jar
*.war
*.ear
target/
.gradle/
build/
.settings/
.classpath
.projectReact/Next.js Project
node_modules/
.next/
out/
build/
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
coverage/Ignoring Already Tracked Files
If a file is already tracked by Git, adding it to .gitignore will not remove it. You need to untrack it first:
# Remove file from tracking but keep it locally
git rm --cached filename.txt
# Remove entire directory from tracking
git rm -r --cached node_modules/
# Commit the removal
git commit -m "Remove tracked files that should be ignored"Remove All Ignored Files from Tracking
# Remove all cached files
git rm -r --cached .
# Re-add everything (gitignore will now take effect)
git add .
# Commit the changes
git commit -m "Apply .gitignore rules to tracked files"Checking Ignore Rules
Test If a File Is Ignored
git check-ignore -v filename.txt.gitignore:3:*.txt filename.txt
This shows which rule in which file is causing the ignore.
List All Ignored Files
git status --ignoredOn branch main
Ignored files:
(use "git add -f <file>..." to include in what will be committed)
.env
logs/
node_modules/Force Adding Ignored Files
Sometimes you need to track a file that matches an ignore pattern:
# Force add an ignored file
git add -f config/.env.example
# Or use negation in .gitignore
# .gitignore:
# .env*
# !.env.exampleNested .gitignore Files
You can have .gitignore files in subdirectories. Rules apply relative to the directory containing the .gitignore:
Interview Questions
- How do you ignore a file that is already being tracked by Git?
First remove it from the index with git rm --cached filename, then add the pattern to .gitignore, and commit the changes. The file remains on disk but is no longer tracked.
- What is the difference between
.gitignoreand.git/info/exclude?
.gitignore is committed to the repository and shared with all collaborators. .git/info/exclude is local-only and not shared — use it for personal ignore rules that should not affect others.
- How do you ignore all files in a directory but keep the directory itself?
Git does not track empty directories. Add a .gitkeep file inside the directory, then in .gitignore use dir/* followed by !dir/.gitkeep.
- What does the
pattern mean in.gitignore?**
Double asterisk matches directories recursively. For example, /logs matches a logs directory anywhere in the repository, and logs/ matches everything inside the logs directory.
- How can you create a global gitignore that applies to all repositories?
Create a file (e.g., ~/.gitignore_global) and configure Git with git config --global core.excludesFile ~/.gitignore_global. Add OS and editor-specific patterns there.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Git Ignore - Exclude Files from Version Control.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Git & GitHub topic.
Search Terms
git-github, git & github, git, github, basics, ignore, git ignore - exclude files from version control
Related Git & GitHub Topics