What is .gitignore in Git
The .gitignore file is used in Git repositories to specify files and directories that Git should ignore and not track. When files are ignored, they are excluded from Git operations such as staging, committing, and pushing to remote repositories.
This file is especially important for preventing temporary files, compiled code, dependencies, and sensitive configuration files from being committed into version control.
A .gitignore file is simply a plain text file that contains a list of patterns. Each pattern tells Git which files or directories should be ignored.
Example structure of a .gitignore file:
# ignore node dependencies
node_modules/
# ignore environment files
.env
# ignore log files
*.log
Why developers use .gitignore
Developers use .gitignore to keep repositories clean and secure. Many files generated during development are not necessary for version control and may even expose sensitive information.
Common reasons to use .gitignore include:
- Prevent committing sensitive files such as
.envor configuration files. - Ignore build artifacts such as
dist/orbuild/directories. - Exclude dependencies that can be downloaded automatically.
- Avoid committing temporary system files generated by operating systems.
- Reduce repository size and improve repository performance.
For example, Node.js projects often ignore dependency folders:
node_modules/
Similarly, Python projects ignore compiled files:
__pycache__/
*.pyc
Files that should never be committed to Git
Some files should always be ignored because they either contain sensitive data or are automatically generated.
Common examples include:
| File Type | Example |
|---|---|
| Environment files | .env, .env.local |
| Logs | *.log |
| Build artifacts | dist/, build/ |
| Dependencies | node_modules/, vendor/ |
| OS generated files | .DS_Store, Thumbs.db |
| IDE settings | .vscode/, .idea/ |
Example .gitignore entries:
.env
*.log
node_modules/
build/
.DS_Store
.vscode/
How Git decides whether a file is tracked or ignored
Git manages files in three states:
| State | Description |
|---|---|
| Tracked | Files already committed to Git |
| Untracked | Files not yet added to Git |
| Ignored | Files listed in .gitignore |
To check file status:
git status
Example output:
Untracked files:
config.env
logs/debug.log
After adding ignore rules:
*.log
config.env
Git will stop showing these files in the status output.
How .gitignore Works
The .gitignore file works by matching file paths against patterns defined inside it. When Git detects a match, the file is ignored.
Where the .gitignore file should be placed
The .gitignore file is typically placed in the root directory of the repository.
Example:
project/
├── .gitignore
├── src/
├── package.json
└── README.md
However, Git also allows .gitignore files in subdirectories.
Example:
project/
├── .gitignore
├── src/
│ ├── .gitignore
│ └── app.js
The rules inside a .gitignore file apply to the directory where the file exists and its subdirectories.
How Git reads ignore rules
Git processes ignore rules line by line. Each line represents a pattern used to match files or directories.
Example .gitignore:
# ignore log files
*.log
# ignore dependency folders
node_modules/
# ignore environment variables
.env
Rules support pattern matching using wildcards.
Examples:
Ignore all .log files:
*.log
Ignore temporary files:
*.tmp
Ignore all build directories:
build/
Priority rules when multiple .gitignore files exist
Git follows a hierarchy when evaluating ignore rules.
Priority order:
- Command-line ignore patterns.
.gitignorefile in the current directory..gitignorefiles in parent directories..git/info/excludefile.- Global
.gitignoreconfiguration.
Example structure:
project/
├── .gitignore
├── src/
│ └── .gitignore
Rules in src/.gitignore override rules defined in the root .gitignore.
Quick .gitignore Cheat Sheet
Most common .gitignore patterns
| Pattern | Description |
|---|---|
*.log | Ignore all log files |
*.tmp | Ignore temporary files |
*.bak | Ignore backup files |
*.env | Ignore environment files |
node_modules/ | Ignore Node.js dependencies |
dist/ | Ignore distribution builds |
build/ | Ignore build output |
*.pyc | Ignore Python compiled files |
__pycache__/ | Ignore Python cache |
.DS_Store | Ignore macOS system file |
Thumbs.db | Ignore Windows system file |
.vscode/ | Ignore VSCode configuration |
.idea/ | Ignore IntelliJ project settings |
coverage/ | Ignore test coverage reports |
target/ | Ignore Java build artifacts |
Quick examples to ignore files and folders
Ignore all log files:
*.log
Ignore environment configuration:
.env
Ignore a folder:
node_modules/
Ignore multiple directories:
node_modules/
build/
dist/
Ignore temporary files:
*.tmp
*.cache
Ignore IDE configuration:
.vscode/
.idea/
Ignore compiled binaries:
*.class
*.exe
*.dll
Ignore test reports:
coverage/
Ignore operating system files:
.DS_Store
Thumbs.db
Ignoring Files in Git
Git allows you to ignore individual files or groups of files using patterns defined in the .gitignore file. This prevents unwanted files from being added to the repository when using commands such as git add ..
Ignoring files is useful when dealing with configuration files, logs, temporary files, or generated content that should not be version controlled.
Ignore a single file
To ignore a specific file, simply add the filename to the .gitignore file.
Example:
config.env
In this case, Git will ignore the file config.env regardless of where it appears in the repository.
If the file exists inside a specific folder, you can specify the path:
config/settings.json
Ignore multiple files
Multiple files can be ignored by listing them on separate lines inside the .gitignore file.
Example:
config.env
debug.log
local_settings.json
Each line represents a new ignore rule.
Ignore file types using wildcard patterns
You can ignore all files with a specific extension using wildcard patterns.
Example:
*.log
*.tmp
*.bak
This ignores all files ending with .log, .tmp, or .bak.
For example:
error.log
access.log
backup.tmp
will all be ignored.
Ignore temporary files and backup files
Temporary files are commonly created by editors and build tools. These files should usually be ignored.
Example patterns:
*.tmp
*.temp
*.swp
*.bak
*.cache
This ensures temporary and backup files are not tracked by Git.
Ignoring Folders and Directories
Directories often contain generated files or dependencies that should not be committed to the repository.
To ignore directories, add the folder name followed by a forward slash /.
Ignore a specific folder
To ignore a directory:
node_modules/
This ignores the entire node_modules directory and everything inside it.
Example ignored files:
node_modules/package.json
node_modules/library/index.js
Ignore multiple directories
You can ignore several directories by listing them separately.
Example:
node_modules/
build/
dist/
logs/
These directories commonly contain compiled files or dependency packages.
Ignore nested folders
To ignore nested folders regardless of location, use double asterisk **.
Example:
**/cache/
This ignores any directory named cache anywhere in the repository.
Example matches:
src/cache/
project/cache/
app/temp/cache/
Ignore folders only in specific locations
Sometimes you only want to ignore folders located in specific directories.
Example:
logs/
This ignores the logs directory only in the root of the repository.
To ignore logs inside a specific folder:
app/logs/
Wildcards and Pattern Matching in .gitignore
The .gitignore file supports several wildcard characters that allow flexible pattern matching.
These patterns help ignore groups of files based on naming conventions.
Ignore files using asterisk (*)
The asterisk * matches zero or more characters.
Example:
*.log
This ignores all log files.
Example matches:
error.log
server.log
access.log
Ignore directories recursively using **
The double asterisk ** matches directories recursively.
Example:
logs/**
This ignores everything inside the logs directory including subdirectories.
Example matches:
logs/error.log
logs/2024/january/debug.log
logs/archive/old.log
Ignore files using question mark (?)
The question mark ? matches exactly one character.
Example:
file?.txt
This matches:
file1.txt
file2.txt
fileA.txt
But does not match:
file10.txt
Ignore file ranges using square brackets
Square brackets allow matching a range of characters.
Example:
log[0-9].txt
Matches:
log1.txt
log5.txt
log9.txt
Another example:
config[abc].json
Matches:
configa.json
configb.json
configc.json
Excluding Files from .gitignore Rules
Sometimes you may want to ignore a group of files but still include specific files. This can be achieved using the negate pattern !.
Using negate pattern (!) to include files
The exclamation mark ! tells Git to include files that were previously ignored.
Example:
*.log
!important.log
In this example:
Ignored files:
error.log
debug.log
But the file below will be tracked:
important.log
Allow a file inside ignored directory
If a directory is ignored, Git normally ignores everything inside it.
Example:
logs/
However, you can allow specific files:
logs/*
!logs/keep.log
This ignores all files inside logs except keep.log.
Re-include specific files in ignored folders
Example:
*.env
!example.env
This ignores all .env files except the example configuration file.
Example:
.env
.env.local
.env.production
Ignored files:
.env
.env.local
Tracked file:
example.env
Real-World .gitignore Examples
Example .gitignore for Node.js projects
Node.js applications generate dependency folders and log files that should not be tracked in Git.
Example .gitignore for Node.js:
# dependency directory
node_modules/
# logs
npm-debug.log
yarn-debug.log
yarn-error.log
# environment variables
.env
# build output
dist/
build/
# coverage reports
coverage/
# OS files
.DS_Store
Example .gitignore for Python projects
Python applications generate compiled files and cache directories which should be ignored.
Example .gitignore for Python:
# python cache
__pycache__/
# compiled python files
*.pyc
*.pyo
# environment files
.env
.venv/
# distribution files
build/
dist/
# packaging
*.egg-info/
Example .gitignore for Java projects
Java projects usually contain compiled .class files and build directories.
Example .gitignore:
# compiled classes
*.class
# jar files
*.jar
# build directories
target/
build/
# IDE settings
.idea/
.vscode/
Example .gitignore for Docker projects
Docker-based projects often contain environment files and temporary build artifacts.
Example .gitignore:
# environment variables
.env
# docker compose overrides
docker-compose.override.yml
# logs
*.log
# build cache
tmp/
Example .gitignore for frontend web projects
Frontend frameworks such as React, Angular, and Vue generate build artifacts and dependencies.
Example .gitignore:
# dependencies
node_modules/
# production build
dist/
# environment configs
.env
.env.local
# logs
*.log
# cache
.cache/
Ignoring OS and IDE Generated Files
Ignore macOS system files
macOS automatically generates hidden files such as .DS_Store.
Example:
.DS_Store
Ignore Windows system files
Windows generates thumbnail cache files and desktop configuration files.
Example:
Thumbs.db
Desktop.ini
Ignore IDE configuration folders
Integrated development environments create project configuration directories.
Common examples:
.vscode/
.idea/
.settings/
These directories usually contain user-specific configuration settings.
Ignore editor temporary files
Editors sometimes generate temporary swap files.
Examples:
*.swp
*.swo
*.tmp
*.bak
These files should not be tracked in version control.
Fixing .gitignore Not Working
Why ignored files still appear in Git
If a file was previously committed to the repository, Git will continue tracking it even if it is later added to .gitignore.
Example scenario:
git add .env
git commit -m "Add environment file"
Later adding .env to .gitignore will not automatically remove it from Git tracking.
Removing tracked files from Git cache
To stop tracking a file while keeping it in your local directory, remove it from Git's index.
Example:
git rm --cached .env
For directories:
git rm -r --cached node_modules
After removing from cache, commit the changes:
git commit -m "Remove tracked files from repository"
Refreshing ignore rules in existing repositories
If many files were already tracked, you may need to refresh the repository cache.
Example process:
git rm -r --cached .
git add .
git commit -m "Apply updated gitignore rules"
This forces Git to re-evaluate ignore rules.
Advanced .gitignore Usage
Advanced .gitignore patterns allow you to create more precise rules for ignoring files and directories.
Ignore files in specific directories
You can ignore files inside a specific directory using path-based patterns.
Example:
logs/*.log
This ignores log files only inside the logs directory.
Example matches:
logs/debug.log
logs/error.log
Ignore files with multiple extensions
You can ignore multiple file types using wildcard patterns.
Example:
*.log
*.tmp
*.cache
*.bak
This ignores files such as:
debug.log
temp.tmp
session.cache
Ignore everything except certain files
Sometimes you want Git to ignore everything except specific files.
Example:
*
!README.md
!.gitignore
This ignores all files except the README and .gitignore.
Ignore all files except a directory
You can ignore all files but keep a specific directory tracked.
Example:
*
!src/
!src/**
This ensures only files inside the src directory are tracked by Git.
Personal Ignore Rules for Local Repository
Sometimes you may want to ignore files only in your local development environment without affecting other contributors. Git provides a mechanism for defining local ignore rules that are not committed to the repository.
This allows developers to ignore personal configuration files or debug artifacts without modifying the project’s main .gitignore.
Using .git/info/exclude
Git provides a special file called .git/info/exclude that works similarly to .gitignore. Any patterns added here apply only to your local repository.
This file is located inside the .git directory.
Example location:
project/.git/info/exclude
You can open this file and add patterns exactly like a .gitignore file.
Example:
debug.log
local_config.json
temp/
Files matching these patterns will be ignored locally but the rules will not be shared with other developers.
Local ignore rules for personal development
The .git/info/exclude file is useful for ignoring files that exist only on your machine.
Common examples include:
- Local debug logs
- Personal configuration files
- Temporary test files
- Machine-specific environment files
Example entries:
debug.log
test.env
temp_output/
These files remain ignored only in your local repository.
Global .gitignore Configuration
Git also allows you to configure a global ignore file that applies to all repositories on your system. This is useful for ignoring operating system files or editor files that appear in every project.
Create a global gitignore file
You can create a global .gitignore file in your home directory.
Example:
touch ~/.gitignore_global
After creating the file, add common ignore patterns.
Example content:
.DS_Store
Thumbs.db
*.log
*.tmp
*.swp
Configure global ignore patterns
Once the global ignore file is created, configure Git to use it.
Example:
git config --global core.excludesfile ~/.gitignore_global
You can verify the configuration:
git config --global core.excludesfile
After this configuration, Git will apply these ignore rules to all repositories on your machine.
Useful global ignore examples
Typical global ignore rules include operating system files, editor settings, and temporary files.
Example global ignore list:
# macOS
.DS_Store
# Windows
Thumbs.db
Desktop.ini
# editor temp files
*.swp
*.tmp
*.bak
# logs
*.log
These patterns help keep repositories clean across all projects.
Debugging .gitignore Rules
Use git check-ignore command
The git check-ignore command allows you to test whether a file is ignored by Git.
Example:
git check-ignore debug.log
If the file matches an ignore rule, Git will display the matching rule.
Identify which rule ignores a file
To see detailed information about which rule is applied, use the verbose option.
Example:
git check-ignore -v logs/debug.log
Example output:
.gitignore:4:*.log logs/debug.log
This output shows:
- the file containing the rule
- the line number
- the pattern that caused the match
Troubleshoot conflicting ignore patterns
Sometimes multiple ignore rules can conflict with each other. For example, a file may be ignored by one rule and re-included by another.
Example .gitignore rules:
*.log
!important.log
In this case:
Ignored files:
debug.log
error.log
Tracked file:
important.log
Using git check-ignore helps determine exactly which rule applies to a file.
Frequently Asked Questions
1. What is .gitignore used for?
.gitignore is used to tell Git which files and directories should not be tracked or committed to the repository.2. How do I ignore a folder in Git?
To ignore a folder, add the folder name followed by a slash in the .gitignore file, for example node_modules/.3. Why is .gitignore not working?
If a file was already committed to Git before adding it to .gitignore, it will still be tracked. You must remove it from tracking using git rm --cached.4. How do I ignore file types in Git?
You can ignore file types using wildcard patterns. For example *.log ignores all log files.5. Where should .gitignore be placed?
.gitignore is usually placed in the root directory of a Git repository but can also be placed inside subdirectories.Summary
The .gitignore file is an essential component of Git repositories that helps developers control which files should be excluded from version control. By defining ignore patterns, developers can prevent unnecessary files, temporary artifacts, and sensitive configuration files from being committed.
In this guide we covered:
- What
.gitignoreis and how it works - How to ignore files and directories in Git
- Using wildcard patterns and advanced ignore rules
- Real-world
.gitignoreexamples for common projects - Global and local ignore configurations
- Debugging ignore rules using Git tools
Understanding .gitignore patterns allows developers to maintain cleaner repositories, improve collaboration, and avoid accidentally committing unwanted files.


