G
GitIgnore.pro

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.

# Just add to .gitignore
echo "filename.txt" >> .gitignore
git add .gitignore
git commit -m "Ignore filename.txt"

๐Ÿ”„Existing Files (Already Tracked)

Files already committed to Git. Need to untrack first.

# Untrack first, then ignore
git rm --cached filename.txt
echo "filename.txt" >> .gitignore
git add .
git commit -m "Stop tracking filename.txt"

Specific Addition Methods

1Add Single File

Command Line Method:

# Method 1: Direct append
echo "config.js" >> .gitignore
# Method 2: Text editor
nano .gitignore
# Add: config.js

File Editor Method:

Open .gitignore in any editor and add:
config.js

๐Ÿ’ก 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:

# Add multiple files to .gitignore
cat >> .gitignore << 'EOF'
config.js
secrets.json
debug.log
EOF

File Type Patterns:

# Ignore all files of certain types
echo "*.log" >> .gitignore
echo "*.tmp" >> .gitignore
echo "*.cache" >> .gitignore

3Add Entire Folders

โœ… Correct Folder Syntax:

# Ignore entire folder
node_modules/
dist/
build/

Note the trailing slash (/) for folders

โŒ Common Mistakes:

# Wrong - no trailing slash
node_modules
# Wrong - leading slash only
/node_modules

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:

# Step 1: Check if file is tracked
git ls-files | grep filename.txt
# Step 2: Remove from Git tracking (keeps local file)
git rm --cached filename.txt
# Step 3: Add to .gitignore
echo "filename.txt" >> .gitignore
# Step 4: Commit changes
git add .gitignore
git commit -m "Stop tracking filename.txt"

For Multiple Files/Folders:

# Remove entire folder from tracking
git rm -r --cached node_modules/
# Remove all *.log files
git rm --cached *.log

Advanced Pattern Examples

๐Ÿ“‹ Common Patterns

Ignore specific file in any directory:
**/config.js
Ignore file only in root:
/config.js
Ignore all files except one:
*.log
!important.log

๐ŸŽฏ Specific Use Cases

Ignore files with specific extension in folder:
src/*.tmp
Ignore numbered files:
file[0-9].txt
Ignore multiple file types:
*.{log,tmp,cache}

Quick Add by Framework

๐ŸPython

echo "__pycache__/" >> .gitignore
echo "*.pyc" >> .gitignore
echo ".env" >> .gitignore
echo "venv/" >> .gitignore
Full Python template โ†’

๐Ÿ“ฆNode.js

echo "node_modules/" >> .gitignore
echo "*.log" >> .gitignore
echo ".env" >> .gitignore
echo "dist/" >> .gitignore
React/Node template โ†’

๐ŸŽฎUnity

echo "[Ll]ibrary/" >> .gitignore
echo "[Tt]emp/" >> .gitignore
echo "[Ll]ogs/" >> .gitignore
echo "*.tmp" >> .gitignore
Unity template โ†’

Verify Your Additions Work

๐Ÿงช Testing Commands

Check if specific file is ignored:

git check-ignore -v filename.txt

Shows which .gitignore rule matches (if any)

See all ignored files:

git status --ignored

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.

# Solution: Untrack first
git rm --cached filename
git commit -m "Stop tracking filename"

"Pattern not working for files in subdirectories"

Use **/pattern to match files at any depth.

# Wrong: only matches root
config.js
# Correct: matches anywhere
**/config.js