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_Storeon 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:
- Create a folder gitignore-demo
- Initialize a new repository locally using
git initinside that folder
Steps to follow:
- Create two files one.txt (with contents “one”) and two.txt (with contents as “two”)
- Create a file called .gitignore with content as two.txt
- Stage files using command git add .
- Check status using git status

🧠 Quick Summary
.gitignorehelps 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
.gitignorekeeps your repository clean, secure, and professional.