Git Ignore
The .gitignore file tells Git which files and folders it should completely ignore — meaning they will never be tracked, staged, or committed. This is essential for keeping a repository clean by leaving out files that should not be shared or stored in version control.
Common examples of files that should be ignored:
- Secret configuration files (API keys, passwords, database credentials)
- Dependencies and package folders (like
node_modules/) - Auto-generated files (compiled code, build outputs)
- Editor and OS-specific files (like
.DS_Storeon macOS orThumbs.dbon Windows) - Log files
- Temporary files
Creating a .gitignore File
The .gitignore file is simply a plain text file named exactly .gitignore (with a dot at the start, no extension). It is placed in the root folder of the project.
# Create the file on Linux/macOS
touch .gitignore
# On Windows (Git Bash)
touch .gitignore
Then open it in any text editor and add the patterns of files to ignore.
Basic .gitignore Syntax
| Pattern | What it Ignores |
|---|---|
filename.txt | A specific file named filename.txt |
*.log | All files with the .log extension |
build/ | The entire build folder and everything inside it |
*.txt | All .txt files anywhere in the project |
!important.txt | Exception — do NOT ignore important.txt even if *.txt is ignored |
# comment | A comment line — Git ignores this line |
**/logs | Any folder named logs at any depth in the project |
doc/*.txt | All .txt files only inside the doc/ folder (not in subfolders) |
A Practical .gitignore Example
Here is a typical .gitignore file for a Node.js web project:
# Dependencies
node_modules/
# Environment variables (contains secrets)
.env
.env.local
.env.production
# Build output
dist/
build/
# Logs
*.log
npm-debug.log*
# OS files
.DS_Store
Thumbs.db
# Editor files
.vscode/
.idea/
# Temporary files
*.tmp
*.swp
Ignoring a Folder
To ignore an entire folder, add a trailing slash:
node_modules/
logs/
Using Wildcards
# Ignore all .jpg files
*.jpg
# Ignore all files in a specific folder
assets/images/*
# But keep one specific image
!assets/images/logo.png
Global .gitignore — Apply to All Projects
A global .gitignore file can be set up that applies to every Git repository on a machine. This is useful for OS and editor files:
# Create the global gitignore file
touch ~/.gitignore_global
# Tell Git to use it
git config --global core.excludesfile ~/.gitignore_global
Then add common OS/editor patterns to ~/.gitignore_global:
.DS_Store
Thumbs.db
.vscode/
*.swp
Ignoring Already Tracked Files
If a file is already being tracked by Git (i.e., it was committed before the .gitignore was set up), adding it to .gitignore will NOT stop Git from tracking it. The file must be explicitly removed from tracking first:
# Remove the file from tracking (but keep it on disk)
git rm --cached filename.txt
# For a whole folder
git rm --cached -r node_modules/
# Then commit the removal
git commit -m "Remove tracked files that should be ignored"
Checking if a File is Being Ignored
git check-ignore -v filename.txtIf the file is ignored, the output shows which rule in .gitignore is causing it to be ignored:
.gitignore:3:*.txt filename.txtThis means line 3 in .gitignore (the pattern *.txt) is causing filename.txt to be ignored.
Ready-Made .gitignore Templates
GitHub provides ready-made .gitignore templates for different programming languages and frameworks at:
https://github.com/github/gitignore
When creating a new repository on GitHub, an option is presented to automatically add a .gitignore based on the chosen programming language.
Summary
The .gitignore file prevents certain files and folders from being tracked by Git. It should always include secrets, dependencies, build outputs, and OS/editor clutter. Patterns support wildcards (*) and exceptions (!). Files already committed must be removed from tracking with git rm --cached before the ignore rule takes effect.
