Home Coding & Scripting Understanding .gitignore and How to Exclude Files from Tracking

Understanding .gitignore and How to Exclude Files from Tracking

292
0

When working with Git, not every file in our project should be tracked. Some files are temporary, system-generated, or personal to our machine. Tracking these files can clutter our repository and cause problems when sharing our project with others. That’s where .gitignore comes in.

In this post, we’ll learn what a .gitignore file is, why it’s important, and how to use it to keep our Git repository clean and professional.


🧩 What Is a .gitignore File?

A .gitignore file is a special text file that tells Git which files or folders it should ignore — meaning they should not be tracked or committed to our repository.

We create it inside our project folder, and Git uses it to decide what to skip whenever we run:

git status
git add .
git commit

If a file matches a rule in .gitignore, Git pretends it doesn’t exist.


❓ Why Do We Need .gitignore?

Many files should not be committed to a Git repository, such as:

  • Temporary files created by our editor (e.g., VS Code settings)
  • Build or compiled files (e.g., bin/, obj/, .class)
  • System files (e.g., .DS_Store on macOS)
  • Log files (*.log)
  • Environment or secret files (.env)
  • Dependencies downloaded from the internet (e.g., node_modules/)

Tracking these files can:

  • Make our repo larger
  • Slow down Git
  • Reveal sensitive information (like passwords!)
  • Cause conflicts between team members

A .gitignore file solves all of these problems easily.


🛠️ How to Create a .gitignore File

In our project folder, simply create a file named:

.gitignore

Make sure the filename starts with a dot, has no other extensions, and is placed in the root of your repository.

Inside that file, list the names or patterns of the files we want Git to ignore.

Example:

node_modules/
*.log
.env

🧪 Example: Ignoring a Single File

To ignore a specific file, list it directly:

secret.txt

Git will stop tracking this file (if it hasn’t been committed yet).


📁 Example: Ignoring a Folder

To ignore an entire folder:

logs/
build/
temp/

The trailing slash / tells Git it’s a folder.


🔤 Example: Ignoring a Type of File

To ignore all files with the same extension:

*.log
*.tmp
*.bak

The asterisk * works as a wildcard.


🛑 Important: .gitignore Only Works for Untracked Files

If a file is already committed, adding it to .gitignore will NOT remove it from the repository.

To stop Git from tracking a file that was already committed:

git rm --cached <filename>

Then commit the change.

Example:

git rm --cached secret.txt
git commit -m "Stop tracking secret.txt"

Now Git will ignore the file going forward.


📘 Useful .gitignore Patterns (Ready to Copy)

Here are some commonly ignored files:

# Logs
*.log

# OS files
.DS_Store

# Environment files
.env

# Build folders
dist/
build/

# Node dependencies
node_modules/

# Compiled Python files
*.pyc
__pycache__/

# VS Code settings
.vscode/

✨ Creating a .gitignore Automatically (Recommended)

GitHub provides ready-made .gitignore templates for different languages:

  • Python
  • Node.js
  • Java
  • .NET
  • React
  • Angular
  • Flutter

Just search for:
“GitHub gitignore <language>”
or use GitHub’s built-in .gitignore generator when creating a new repository.


🧪 Exercise: Practice Using .gitignore

Prerequisites:

Follow the exercises in the post Git Repository Structure and Workflow Stages to:

  1. Create a folder gitignore-demo
  2. Initialize a new repository locally using git init inside that folder

Steps to follow:

  1. Create two files one.txt (with contents “one”) and two.txt (with contents as “two”)
  2. Create a file called .gitignore with content as two.txt
  3. Stage files using command git add .
  4. Check status using git status

🧠 Quick Summary

  • .gitignore helps exclude unwanted files from your Git repository.
  • You list file names or patterns to tell Git what to ignore.
  • It only works for untracked files — tracked files must be removed using git rm --cached.
  • Using .gitignore keeps your repository clean, secure, and professional.
Previous articleUnderstanding Git Branches
Next articleWhat Is GitHub? History, Key Features, and Alternatives
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here