Back to Blog
Lesson 10 of the Git & GitHub: Git & GitHub from Zero course
GitJuly 28, 20264 min read

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.

gitversion controlgitignoredeveloper toolsterminal
Close-up of software development tools displaying code and version control systems on a computer monitor.

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

Neatly arranged blue office binders labeled with dates and names for organized storage.

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:

Bash
touch .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.log ignores that exact file.
  • Extensions: *.log ignores any file ending in .log.
  • Directories: temp/ ignores the entire temp directory and its contents.
  • Negation: !important.log forces Git to track important.log even if *.log is 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.

  1. Open your .gitignore file in your text editor.
  2. 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
  3. 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

  1. Navigate to your project root.
  2. Create a dummy log file: touch app.log.
  3. Run git status and observe it appearing as an untracked file.
  4. Add *.log to your .gitignore file.
  5. Run git status again. The app.log file 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 .gitignore will not stop Git from tracking it. You must first remove it from the index using git rm --cached config.json before 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

Wooden Scrabble tiles spelling 'RECAP' on a brown textured background. Text concept image.

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.

Similar Posts