How to Add Files to .gitignore
Master every method to add files to .gitignore: new files, existing tracked files, folders, and file types. Learn git rm --cached and prevention techniques.
Choose Your Method
โจNew Files (Not Yet Tracked)
Files that haven't been committed to Git yet. Simple to ignore.
๐Existing Files (Already Tracked)
Files already committed to Git. Need to untrack first.
Specific Addition Methods
1Add Single File
Command Line Method:
File Editor Method:
๐ก Tip: Use relative paths from the repository root. For files in subdirectories, use src/config.js
or **/config.js
.
2Add Multiple Files at Once
Multiple Specific Files:
File Type Patterns:
3Add Entire Folders
โ Correct Folder Syntax:
Note the trailing slash (/) for folders
โ Common Mistakes:
Without / it might match files too
4Add Already Tracked Files (Most Common Issue)
โ ๏ธ Important: If files are already tracked by Git, simply adding them to .gitignore won't work. You must untrack them first.
Step-by-Step Process:
For Multiple Files/Folders:
Advanced Pattern Examples
๐ Common Patterns
๐ฏ Specific Use Cases
Quick Add by Framework
๐Python
๐ฆNode.js
๐ฎUnity
Verify Your Additions Work
๐งช Testing Commands
Check if specific file is ignored:
Shows which .gitignore rule matches (if any)
See all ignored files:
Lists all files being ignored by Git
๐ก Pro Tip: Use ourPattern Validator tool to test your .gitignore patterns before committing.
โ ๏ธ Common Issues & Solutions
"I added to .gitignore but Git still tracks the file"
This happens when the file was already tracked before adding to .gitignore.
"Pattern not working for files in subdirectories"
Use **/pattern
to match files at any depth.