Exercise: Ignoring Files
Practice creating and using .gitignore files.
Setup
bash
1mkdir ignore-practice
2cd ignore-practice
3git init
4
5echo "# Ignore Practice" > README.md
6git add README.md
7git commit -m "Initial commit"Exercise 1: Create Basic .gitignore
Task
Create a .gitignore that ignores log files and the node_modules directory.
Steps
bash
1# Create .gitignore
2cat > .gitignore << EOF
3# Logs
4*.log
5
6# Dependencies
7node_modules/
8EOF
9
10# Verify it exists
11cat .gitignore
12
13# Stage and commit
14git add .gitignore
15git commit -m "Add .gitignore"Exercise 2: Test File Ignoring
Task
Verify that ignored files don't show in git status.
Steps
bash
1# Create files that should be ignored
2echo "Error log" > error.log
3echo "Debug log" > debug.log
4mkdir node_modules
5echo "package" > node_modules/package.js
6
7# Create a file that should NOT be ignored
8echo "console.log('app');" > app.js
9
10# Check status
11git status
12
13# You should only see app.js, not the logs or node_modulesExpected Output
Untracked files:
app.js
Exercise 3: Ignore Environment Files
Task
Add patterns to ignore environment files.
Steps
bash
1# Add to .gitignore
2cat >> .gitignore << EOF
3
4# Environment files
5.env
6.env.local
7.env.*.local
8EOF
9
10# Create environment files
11echo "SECRET_KEY=abc123" > .env
12echo "LOCAL_VAR=true" > .env.local
13echo "DEV_MODE=true" > .env.development.local
14
15# Check status - they should be ignored
16git statusExercise 4: Ignore IDE/Editor Files
Task
Add patterns for common IDE files.
Steps
bash
1# Add IDE patterns
2cat >> .gitignore << EOF
3
4# IDE/Editor
5.vscode/
6.idea/
7*.sublime-*
8*.swp
9*~
10EOF
11
12# Create IDE files
13mkdir .vscode
14echo '{"setting": true}' > .vscode/settings.json
15mkdir .idea
16echo "config" > .idea/workspace.xml
17
18# Verify they're ignored
19git status
20git check-ignore -v .vscode/settings.jsonExercise 5: Use Negation Pattern
Task
Ignore all .log files except important.log.
Steps
bash
1# Update .gitignore
2cat >> .gitignore << EOF
3
4# Ignore all logs except important
5*.log
6!important.log
7EOF
8
9# Create log files
10echo "Regular log" > regular.log
11echo "IMPORTANT" > important.log
12
13# Check status
14git status
15# Only important.log should show as untrackedExercise 6: Ignore Build Directories
Task
Ignore common build output directories.
Steps
bash
1# Add build patterns
2cat >> .gitignore << EOF
3
4# Build outputs
5dist/
6build/
7out/
8.cache/
9EOF
10
11# Create build directories
12mkdir -p dist build out .cache
13echo "bundle.js" > dist/bundle.js
14echo "output" > build/output.txt
15
16# Verify they're ignored
17git statusExercise 7: Stop Tracking Already Tracked File
Task
You accidentally committed a file that should be ignored. Fix it.
Steps
bash
1# Accidentally track a file
2echo "SECRET=password123" > secrets.txt
3git add secrets.txt
4git commit -m "Oops, added secrets"
5
6# Add to .gitignore
7echo "secrets.txt" >> .gitignore
8
9# Check status - file still tracked!
10git status
11# secrets.txt is NOT showing as ignored
12
13# Remove from tracking (keep the file)
14git rm --cached secrets.txt
15
16# Now commit the removal
17git add .gitignore
18git commit -m "Remove secrets.txt from tracking"
19
20# Verify
21git status
22# secrets.txt should now be ignoredExercise 8: Check Why File is Ignored
Task
Use git check-ignore to debug ignore patterns.
Steps
bash
1# Create various files
2echo "test" > test.log
3echo "test" > test.txt
4mkdir temp && echo "file" > temp/file.txt
5
6# Add patterns
7echo "temp/" >> .gitignore
8
9# Check which pattern ignores each file
10git check-ignore -v test.log
11git check-ignore -v temp/file.txt
12
13# Check a file that's NOT ignored
14git check-ignore -v test.txt
15# No output = not ignoredChallenge: Complete Project .gitignore
Task
Create a comprehensive .gitignore for a Node.js/React project.
Solution
bash
1cat > .gitignore << 'EOF'
2# Dependencies
3node_modules/
4.pnp/
5.pnp.js
6
7# Build outputs
8dist/
9build/
10.next/
11out/
12
13# Testing
14coverage/
15
16# Environment
17.env
18.env.local
19.env.development.local
20.env.test.local
21.env.production.local
22
23# Logs
24npm-debug.log*
25yarn-debug.log*
26yarn-error.log*
27
28# IDE/Editor
29.vscode/
30.idea/
31*.sublime-*
32*.swp
33*~
34
35# OS
36.DS_Store
37Thumbs.db
38
39# Misc
40.cache/
41*.tmp
42*.temp
43EOF
44
45git add .gitignore
46git commit -m "Add comprehensive .gitignore"Self-Check
View your complete .gitignore:
bash
1cat .gitignoreTest that files are properly ignored:
bash
1git statusSummary
You've practiced:
- ✅ Creating
.gitignorefiles - ✅ Using different patterns (wildcards, directories)
- ✅ Using negation patterns
- ✅ Stopping tracking of already-tracked files
- ✅ Debugging with
git check-ignore
Great work!
