Learn how to effectively exclude files from version control using .gitignore
Learn how to use .gitignore to exclude files from version control, with common patterns and best practices.
Not every file in your project directory needs to be version controlled. Build artifacts, dependencies, and personal IDE settings are examples of files that should typically be ignored. The .gitignore file tells Git which files and directories to ignore.
- •
Build artifacts and compiled code
- •
Dependencies and package directories
- •
Environment and configuration files
- •
IDE and editor specific files
- •
Operating system files
- •
Keeps repositories clean and focused
- •
Prevents sensitive information leaks
- •
Improves repository performance
- •
Reduces conflicts between environments
# Ignore specific file
config.json
# Ignore file type
*.log
# Ignore directory
node_modules/
# Ignore path pattern
build/
dist/
# Negating pattern (don't ignore)
!important.log
# Ignore files in any directory
**/temp/
# Complex patterns
docs/*.md
!docs/README.md
- Blank lines are ignored
- Lines starting with # are comments
- Trailing spaces are ignored
- Patterns are matched relative to .gitignore location
Practice creating ignore patterns with this interactive demonstration:
- Project-specific ignore rules
- Committed to repository
- Shared with team members
- Located in project root
# In project root
touch .gitignore
- System-wide ignore rules
- Personal preferences
- IDE/editor specific
- Not committed to repositories
# Configure global gitignore
git config --global core.excludesfile ~/.gitignore_global
- #### Create Early
Add .gitignore before your first commit to avoid tracking unwanted files.
- #### Be Specific
Use precise patterns to avoid accidentally ignoring important files.
- #### Document Patterns
Add comments to explain non-obvious ignore patterns.
If a file is already tracked, adding it to .gitignore won't stop tracking it. Solution:
git rm --cached
Git doesn't track empty directories. To keep an empty directory, add a .gitkeep file.
Later patterns override earlier ones. Place more specific patterns after general ones.
Now that you understand how to manage ignored files, you'll learn about:
- Different branching strategies
- Basic branch operations and management