Managing Untracked Files in Git
A .gitignore file is a special text file in your Git repository that tells Git which files or directories to ignore and not track. When Git sees a file or pattern listed in .gitignore, it will not include those files in version control.
.gitignoreUsing a .gitignore file is crucial for maintaining a clean and efficient Git repository. Here are the main reasons:
Prevent unnecessary files from cluttering your repository history.
Avoid accidentally committing sensitive information like:
Ignore large or frequently changing files that don't need version control:
Prevent merge conflicts from user-specific or machine-specific files:
# On Linux/macOS
touch .gitignore
# On Windows (Command Prompt)
echo. > .gitignore
# On Windows (PowerShell)
New-Item .gitignore
.gitignore in your repository rootWhen creating a new repository on GitHub, you can choose a .gitignore template for your project type (Python, Node.js, Java, etc.). GitHub provides pre-made templates for common languages and frameworks.
github.com/github/gitignore to browse official .gitignore
templates for different programming languages and frameworks.
The .gitignore file uses pattern matching syntax to specify which files to ignore:
| Pattern | Description | Example |
|---|---|---|
# |
Comment line (ignored by Git) | # This is a comment |
| Blank lines | Ignored (used for readability) | |
filename |
Ignore specific file | secret.txt |
*.ext |
Ignore all files with extension | *.log |
folder/ |
Ignore entire directory | node_modules/ |
**/pattern |
Match in all directories | **/logs |
! |
Negate pattern (don't ignore) | !important.log |
? |
Match single character | file?.txt |
[abc] |
Match any character in brackets | file[123].txt |
* - Matches zero or more characters (except /)** - Matches zero or more directories? - Matches exactly one character[abc] - Matches any one character from the set[0-9] - Matches any one digit# Ignore a specific file
config.txt
# Ignore all .log files
*.log
# Ignore all .tmp files
*.tmp
# Ignore node_modules directory
node_modules/
# Ignore build directory
build/
# Ignore all .txt files in logs/ directory only
logs/*.txt
# Ignore all files in temp/ directory
temp/*
# Ignore all .log files
*.log
# But don't ignore important.log
!important.log
# Ignore all logs directories anywhere in the project
**/logs/
# Ignore all .cache files anywhere
**/*.cache
# Ignore test1.txt, test2.txt, test3.txt
test[123].txt
# Ignore all files starting with temp and one more char
temp?.log
# macOS
.DS_Store
.AppleDouble
.LSOverride
# Windows
Thumbs.db
ehthumbs.db
Desktop.ini
# Linux
*~
.directory
# Visual Studio Code
.vscode/
*.code-workspace
# IntelliJ IDEA
.idea/
*.iml
# Sublime Text
*.sublime-project
*.sublime-workspace
# Vim
*.swp
*.swo
*~
# Emacs
*~
\#*\#
# Node.js
node_modules/
npm-debug.log
yarn-error.log
# Python
__pycache__/
*.py[cod]
venv/
env/
# Java
target/
*.class
# .NET
bin/
obj/
# Compiled files
*.exe
*.dll
*.so
*.dylib
*.o
*.a
# Archives
*.zip
*.tar
*.tar.gz
*.rar
# Byte-compiled / optimized
__pycache__/
*.py[cod]
*$py.class
# Virtual environments
venv/
env/
ENV/
# Distribution / packaging
dist/
build/
*.egg-info/
# Testing
.pytest_cache/
.coverage
htmlcov/
# Environment variables
.env
# Dependencies
node_modules/
npm-debug.log
yarn-error.log
yarn.lock
package-lock.json
# Build output
dist/
build/
out/
# Environment
.env
.env.local
# Testing
coverage/
# Compiled class files
*.class
# Package Files
*.jar
*.war
*.ear
# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
# Gradle
.gradle/
build/
# Compiled Object files
*.o
*.obj
# Executables
*.exe
*.out
*.app
# Libraries
*.lib
*.a
*.so
*.dll
# CMake
CMakeCache.txt
CMakeFiles/
A global .gitignore file applies to all Git repositories on your computer. It's useful for ignoring user-specific or OS-specific files that you never want to track.
# Create file in home directory
touch ~/.gitignore_global
git config --global core.excludesfile ~/.gitignore_global
# Operating System Files
.DS_Store
Thumbs.db
# Editor Files
.vscode/
.idea/
*.swp
# Personal Notes
TODO.md
NOTES.md
Cause: Files are already tracked by Git.
Solution: Remove cached files from Git's tracking:
# Remove specific file from tracking
git rm --cached filename
# Remove directory from tracking
git rm -r --cached directory/
# Remove all files and re-add (apply new .gitignore)
git rm -r --cached .
git add .
git commit -m "Apply .gitignore rules"
Steps:
git rm --cached filenameSolution: Use negation pattern:
# Ignore entire logs directory
logs/
# But track important.log
!logs/important.log
Command: Check if a file would be ignored:
git check-ignore -v filename
This shows which pattern in .gitignore is matching the file.
Make sure .gitignore doesn't have a hidden extension like .gitignore.txt
Check:
# On Linux/macOS
ls -la | grep gitignore
# Should show: .gitignore (not .gitignore.txt)
# ===========================
# Project: My Web Application
# ===========================
# Dependencies
node_modules/
vendor/
# Build Output
dist/
build/
*.min.js
*.min.css
# Environment Variables
.env
.env.local
.env.production
# Logs
logs/
*.log
npm-debug.log*
# Testing
coverage/
.nyc_output/
# IDE
.vscode/
.idea/
# OS Files
.DS_Store
Thumbs.db
# Temporary Files
*.tmp
*.temp
.cache/
git check-ignore