← Back to Chapters

Git .gitignore

? Git .gitignore

? Quick Overview

The .gitignore file tells Git which files or directories to ignore in a repository. It prevents unnecessary files like logs, build outputs, or IDE configs from being tracked.

? Key Concepts

  • Prevents unwanted files from being committed
  • Helps keep repositories clean
  • Essential for team collaboration

? What is a .gitignore File?

The .gitignore file contains patterns that Git should ignore. These patterns apply to files not yet tracked.

❓ Why Use .gitignore?

  • Keep the repository clean
  • Protect sensitive data
  • Avoid IDE-specific clutter
  • Improve performance

? How to Create a .gitignore File

? View Code Example
# Ignore all .log files
*.log
# Ignore build directory
build/
# Ignore IDE files
.vscode/
.idea/

? Basic Syntax

  • # for comments
  • * wildcard for patterns
  • / at end to ignore directories
  • ! to negate ignores

? Common Use Cases

  • IDE configuration files
  • Build and dist folders
  • Logs and temp files

? Node.js Example

? View Code Example
# Node dependencies
node_modules/
# Debug logs
npm-debug.log*

☕ Java Example

? View Code Example
# Compiled class files
*.class
# IDE folders
.idea/
.vscode/

? Apply .gitignore to Existing Repo

? View Code Example
# Untrack already tracked files
git rm -r --cached file-or-directory
? View Code Example
# Commit .gitignore changes
git commit -m "Update .gitignore"

? Tips & Best Practices

  • Always check git status
  • .gitignore affects only untracked files
  • Use generators like gitignore.io

? Try It Yourself

  • Create a .gitignore file
  • Add patterns like node_modules/
  • Untrack files using git rm --cached