Ignoring Files: Mastering .gitignore for Cleaner Repositories
Learn how to use .gitignore to prevent junk files, logs, and build artifacts from cluttering your repository and bloating your history.

Previously in this course, we covered Tracking Files: How to Use git add for Version Control, where you learned how to move files into the staging area. But what happens when your project generates files you don't want to track?
Every software project eventually produces "noise": temporary log files, local IDE configuration folders, or heavy build artifacts. If you add these to Git, you’ll bloat your repository, create merge conflicts for your teammates, and potentially expose sensitive local settings. This lesson teaches you how to silence that noise using the .gitignore file.
Why We Need File Ignoring
Git tracks every file you tell it to track. However, modern development environments are messy. If you are working on a web project, you might have a node_modules folder with 50,000 files, or debug.log files that change every time you run your code.
Tracking these files makes your git status output unreadable and your repository unnecessarily large. The .gitignore file acts as a filter, telling Git: "I know these files exist in my folder, but please act as if they aren't there."
Creating Your First .gitignore File

A .gitignore file is simply a plain text file named exactly .gitignore (note the leading dot) placed in the root of your project directory.
To create one, use your terminal:
Bashtouch .gitignore
Once the file exists, you define patterns inside it. Each line represents a rule. Git reads this file top-to-bottom and ignores any file or folder matching the patterns you define.
Defining Patterns for File Tracking
The syntax is straightforward, but there are a few conventions you should master:
- Specific files:
debug.logignores that exact file. - Extensions:
*.logignores any file ending in.log. - Directories:
temp/ignores the entiretempdirectory and its contents. - Negation:
!important.logforces Git to trackimportant.logeven if*.logis ignored.
Worked Example: Cleaning Up Our Project
Let's apply this to our course project. Suppose your project is generating a local folder called logs and a file named local-settings.json that contains your private API keys.
- Open your
.gitignorefile in your text editor. - Add the following lines:
TEXT
# Ignore all log files logs/ # Ignore local configuration local-settings.json # Ignore OS-specific clutter (common for macOS) .DS_Store - Save the file.
Now, run git status. You will notice that git status no longer lists these files as "untracked," even if they exist in your directory. Git is effectively ignoring them.
Hands-On Exercise
- Navigate to your project root.
- Create a dummy log file:
touch app.log. - Run
git statusand observe it appearing as an untracked file. - Add
*.logto your.gitignorefile. - Run
git statusagain. Theapp.logfile should now be invisible to Git.
Common Pitfalls
- Ignoring already tracked files: If you have already committed a file (e.g.,
config.json), adding it to.gitignorewill not stop Git from tracking it. You must first remove it from the index usinggit rm --cached config.jsonbefore the ignore rule takes effect. - Forgetting the leading dot: If you name the file
gitignore(without the dot), Git will ignore the file itself, but it won't use it as a configuration file. - Over-ignoring: Be careful with broad patterns like
*. It’s easy to accidentally ignore your entire project source code if you aren't specific with your extensions.
FAQ
Does .gitignore affect my computer's file system? No. It only affects how Git interacts with those files. Your files remain on your hard drive, completely untouched.
Can I have multiple .gitignore files?
Yes. You can place a .gitignore file in subdirectories to apply rules specific to that folder. However, for beginners, we recommend sticking to one root-level .gitignore to keep things simple.
Is .gitignore shared with teammates?
Yes. Since .gitignore is a file in your repository, it is versioned just like your code. When you commit and push it, your teammates will automatically use the same ignore rules you defined.
Recap

You’ve now taken control of your repository's hygiene. By creating a .gitignore file and defining specific patterns, you ensure that only your actual source code—not the transient build artifacts or logs—is tracked. This keeps your project history clean and professional.
Up next: We will learn how to recover from mistakes by exploring how to discard uncommitted changes using git restore.
Work with me

Custom WordPress Plugin Development
Custom WordPress & WooCommerce plugins built to standard — by the developer behind a plugin with 5,000+ active installs and a SaaS with 10,000+ users.

WooCommerce Store Setup & Customization
A WooCommerce store that's set up right, customized to your brand, and ready to sell — by a developer who builds WooCommerce products, not just stores.

