Beginner LevelGit & GitHub
How does .gitignore exclude untracked files and build artifacts from Git history?
❓ Interview Prompt: Explain .gitignore syntax, glob matching patterns, wildcards, directory exclusion, and ignoring tracked files.
The '.gitignore' file lists file patterns that Git should intentionally ignore and exclude from untracked file lists.
Common Glob Patterns:
•
'node_modules/': Ignores entire directory and all subdirectories.
•
'*.log': Ignores all files ending in '.log'.
•
'.env*': Ignores '.env', '.env.local', etc., preventing sensitive API secrets from being accidentally committed!
•
'!important.log': Negation rule (do NOT ignore 'important.log').
Important Note:
If a file was already committed to Git history previously, adding it to '.gitignore' later will NOT stop Git from tracking it! You must untrack it first using 'git rm --cached <file>'.
Code Implementation Example
ini
# Root .gitignore configuration
node_modules/
dist/
build/
*.log
.env
.env.local
.DS_Store💡Standard .gitignore configuration excluding dependencies, build outputs, and secrets.
🎯 Interview Key Takeaway
Use .gitignore glob patterns to prevent committing build outputs and secrets. Untrack existing files with git rm --cached.
#.gitignore#File Exclusion#Security#Beginner
Back to all Git & GitHub Questions→