
Module 2 Lesson 4: .gitignore and ignoring files
Not every file belongs in history. Learn how to use .gitignore to keep temporary files, sensitive secrets, and heavy dependencies out of your repository.
Module 2 Lesson 4: .gitignore and ignoring files
In every software project, there are files you never want Git to track.
- Temporary Files: Like logs or build artifacts (
dist/,build/). - Dependency Folders: Like
node_modules. - Secrets: Like
.envfiles containing database passwords. - OS-specific Junk: Like
DS_Storeon Mac.
If you don't tell Git to ignore these, they will clutter your history and can even lead to major security risks. This is where the .gitignore file comes in.
1. What is a .gitignore?
A .gitignore file is a plain text file you place at the root of your project. Each line in the file tells Git to ignore certain files or patterns.
Crucially, Git ignore only works for "Untracked" files. If you have already committed a file to Git, adding it to .gitignore later won't stop Git from tracking it. You'd have to remove it from the repository first.
2. Common Patterns and Syntax
You can use "Globs" (wildcards) to match files:
# Ignore a specific file
secrets.txt
# Ignore all files ending in .log
*.log
# Ignore an entire directory (and everything in it)
node_modules/
temp/
# Ignore all files in a folder, but KEEP the folder (by keeping one file)
# This is a common trick
log/*.log
The "Negative" Rule
Sometimes you want to ignore a whole folder, but keep one specific file. Use the ! prefix:
# Ignore all zip files
*.zip
# But do NOT ignore the backup.zip file
!backup.zip
3. Best Practices
Use Templates
You don't have to write your .gitignore from scratch. Websites like gitignore.io allow you to Type in your tools (e.g., "Node, React, macOS, VSCode") and generate a perfect file for you.
Global vs. Local
- Local .gitignore: Dedicated to the project. This should be committed to the repository so everyone on the team ignores the same things.
- Global .gitignore: Dedicated to your computer. Use this for OS-level junk like
DS_Storeso you don't have to add it to every single project.- Setup:
git config --global core.excludesfile ~/.gitignore_global
- Setup:
graph TD
Folder["Project Folder"] --> FileA["Code (Tracked)"]
Folder --> FileB["Config (Tracked)"]
Folder --> FileC[".env (Ignored via .gitignore)"]
Folder --> FileD["node_modules/ (Ignored via .gitignore)"]
FileA --> Commit["Git Record"]
FileB --> Commit
FileC -- "X" --> Commit
FileD -- "X" --> Commit
Module 2 Practice Exercises
Let's test your repository management skills:
- Initialize: Create a new folder
module-2-testand initialize it. - Setup Ignored Files: Create a file called
logs.txtand a directory calledtmp/. - Configure .gitignore: Create a
.gitignorefile and addlogs.txtandtmp/to it. - Verify: Use
git status. Do thelogs.txtortmp/files appear under "Untracked files"? (They shouldn't!). - Inspect the Config: Peek inside the
.git/configfile. Notice it’s currently minimalist since we haven't added a remote yet.
Checkpoint: If your git status output is clean even though your folder has "junk" files, you have successfully set up your repository defenses!
Summary
In this lesson, we established:
- Why ignoring files is essential for security and repository health.
- The syntax of
.gitignore(wildcards, directories, negations). - That
.gitignoreonly applies to files that aren't already being tracked. - The difference between local and global ignore settings.
Module Complete! You know how to create repos, clone them, understand their internal brain, and defend them with ignore rules.
Next Module: We’ll actually start saving work. Welcome to Module 3: Basic Git Workflow, where we'll master the "Add, Commit, Log" cycle.