Ignoring Files with .gitignore
You'll learn how to tell Git to deliberately ignore certain files and directories so they never clutter your repository.
Why Ignore Files?
Not everything in your project folder belongs in version control. Build artifacts (compiled binaries, .class files, .o objects), dependency folders (node_modules/, venv/), editor configurations (.vscode/, .idea/), and sensitive files (API keys, passwords) should never be tracked. They're either auto-generated, user-specific, or dangerous to share.
Think of .gitignore as a "do not disturb" list for Git—files and patterns you list there become invisible to git add and git status, keeping your repository clean and secure.
Creating a .gitignore File
Simply create a file named .gitignore in your repository's root directory. Each line specifies a pattern:
# Compiled output
*.class
*.o
build/
# Dependencies
node_modules/
# Environment files
.env
config/secrets.yml
# Editor files
.vscode/
*.swp
Pattern Rules
file.txt— ignores that specific file*.log— ignores all files ending in.logbuild/— ignores the entirebuilddirectory!important.log— negates a previous pattern (don't ignore this one)#— comments
Git checks .gitignore before staging files. If a file matches a pattern, Git treats it as if it doesn't exist.
Important Notes
Files already tracked aren't affected by adding them to .gitignore later—you must first untrack them with git rm --cached filename. The .gitignore file itself should be tracked and committed so everyone on the team ignores the same files.
Key Takeaway: Use .gitignore to exclude generated, temporary, and sensitive files from version control, keeping your repository focused on source code and intentional assets.