Module 2 Lesson 4: .gitignore and ignoring files
·DevOps

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 .env files containing database passwords.
  • OS-specific Junk: Like DS_Store on 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_Store so you don't have to add it to every single project.
    • Setup: git config --global core.excludesfile ~/.gitignore_global
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:

  1. Initialize: Create a new folder module-2-test and initialize it.
  2. Setup Ignored Files: Create a file called logs.txt and a directory called tmp/.
  3. Configure .gitignore: Create a .gitignore file and add logs.txt and tmp/ to it.
  4. Verify: Use git status. Do the logs.txt or tmp/ files appear under "Untracked files"? (They shouldn't!).
  5. Inspect the Config: Peek inside the .git/config file. 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 .gitignore only 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.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn