.gitignore - Managing Ignored Files

Learn how to effectively exclude files from version control using .gitignore

Learning Objectives

  • Understand the purpose and importance of .gitignore
  • Learn how to create and modify .gitignore files
  • Master different ignore patterns and their use cases
  • Understand global vs local ignore rules

Understanding .gitignore

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.

Common Ignore Categories

  • Build artifacts and compiled code
  • Dependencies and package directories
  • Environment and configuration files
  • IDE and editor specific files
  • Operating system files

Benefits

  • Keeps repositories clean and focused
  • Prevents sensitive information leaks
  • Improves repository performance
  • Reduces conflicts between environments

Ignore Patterns

Pattern Types and Examples

Common .gitignore patterns
# 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

Pattern Rules

  • • Blank lines are ignored
  • • Lines starting with # are comments
  • • Trailing spaces are ignored
  • • Patterns are matched relative to .gitignore location

Try It Yourself

Practice creating ignore patterns with this interactive demonstration:

Add Ignore Pattern

.gitignore Contents

No patterns added yet

Project Files

index.html
styles.css
node_modules
main.js
.env
build
.DS_Store
*.log

Git Status

Add some ignore patterns to see what files would be ignored.

Global vs Local .gitignore

Local .gitignore

  • Project-specific ignore rules
  • Committed to repository
  • Shared with team members
  • Located in project root
# In project root
touch .gitignore

Global .gitignore

  • System-wide ignore rules
  • Personal preferences
  • IDE/editor specific
  • Not committed to repositories
# Configure global gitignore
git config --global core.excludesfile ~/.gitignore_global

Best Practices

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

Common Issues and Solutions

Already Tracked Files

If a file is already tracked, adding it to .gitignore won't stop tracking it. Solution:

git rm --cached <file>

Empty Directories

Git doesn't track empty directories. To keep an empty directory, add a .gitkeep file.

Pattern Order

Later patterns override earlier ones. Place more specific patterns after general ones.

What's Next?

Now that you understand how to manage ignored files, you'll learn about:

  • Different branching strategies
  • Basic branch operations and management