Understanding .gitignore

Managing Untracked Files in Git

A Comprehensive Guide

Git & GitHub Workshop

NEUB CSE Society

Prepared by: Mahfuz Ibne Syful

Table of Contents

1. What is .gitignore?

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.

Key Points:

📌 Note: The dot (.) at the beginning of the filename makes it a hidden file on Unix-based systems (Linux, macOS). On Windows, you may need to enable viewing hidden files to see it.

2. Why Use .gitignore?

Using a .gitignore file is crucial for maintaining a clean and efficient Git repository. Here are the main reasons:

1. Keep Repository Clean

Prevent unnecessary files from cluttering your repository history.

2. Security

Avoid accidentally committing sensitive information like:

3. Performance

Ignore large or frequently changing files that don't need version control:

4. Avoid Conflicts

Prevent merge conflicts from user-specific or machine-specific files:

⚠ Important: Never commit sensitive credentials or API keys to Git. Once committed, they remain in the repository history even if you delete them later. Use .gitignore to prevent this.

3. Creating a .gitignore File

Method 1: Using Terminal/Command Line

# On Linux/macOS
touch .gitignore

# On Windows (Command Prompt)
echo. > .gitignore

# On Windows (PowerShell)
New-Item .gitignore

Method 2: Using a Text Editor

  1. Open your text editor or IDE
  2. Create a new file
  3. Save it as .gitignore in your repository root
  4. Make sure it doesn't have any extension (not .gitignore.txt)

Method 3: Using GitHub

When 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.

💡 Tip: Visit github.com/github/gitignore to browse official .gitignore templates for different programming languages and frameworks.

4. Basic Syntax and Patterns

The .gitignore file uses pattern matching syntax to specify which files to ignore:

Basic Rules:

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

Wildcards:

5. Pattern Examples

Example 1: Ignore Specific File

# Ignore a specific file
config.txt

Example 2: Ignore All Files with Extension

# Ignore all .log files
*.log

# Ignore all .tmp files
*.tmp

Example 3: Ignore Directory

# Ignore node_modules directory
node_modules/

# Ignore build directory
build/

Example 4: Ignore Files in Specific Directory

# Ignore all .txt files in logs/ directory only
logs/*.txt

# Ignore all files in temp/ directory
temp/*

Example 5: Negation (Exception)

# Ignore all .log files
*.log

# But don't ignore important.log
!important.log

Example 6: Match Nested Directories

# Ignore all logs directories anywhere in the project
**/logs/

# Ignore all .cache files anywhere
**/*.cache

Example 7: Character Sets

# Ignore test1.txt, test2.txt, test3.txt
test[123].txt

# Ignore all files starting with temp and one more char
temp?.log

6. Common .gitignore Patterns

Operating System Files

# macOS
.DS_Store
.AppleDouble
.LSOverride

# Windows
Thumbs.db
ehthumbs.db
Desktop.ini

# Linux
*~
.directory

IDE and Editor Files

# Visual Studio Code
.vscode/
*.code-workspace

# IntelliJ IDEA
.idea/
*.iml

# Sublime Text
*.sublime-project
*.sublime-workspace

# Vim
*.swp
*.swo
*~

# Emacs
*~
\#*\#

Build and Dependency Directories

# Node.js
node_modules/
npm-debug.log
yarn-error.log

# Python
__pycache__/
*.py[cod]
venv/
env/

# Java
target/
*.class

# .NET
bin/
obj/

Compiled Files and Archives

# Compiled files
*.exe
*.dll
*.so
*.dylib
*.o
*.a

# Archives
*.zip
*.tar
*.tar.gz
*.rar

7. Language/Framework Specific

Python

# 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

Node.js / JavaScript

# 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/

Java

# Compiled class files
*.class

# Package Files
*.jar
*.war
*.ear

# Maven
target/
pom.xml.tag
pom.xml.releaseBackup

# Gradle
.gradle/
build/

C/C++

# Compiled Object files
*.o
*.obj

# Executables
*.exe
*.out
*.app

# Libraries
*.lib
*.a
*.so
*.dll

# CMake
CMakeCache.txt
CMakeFiles/

8. Global .gitignore

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.

Setting Up Global .gitignore:

Step 1: Create the global ignore file

# Create file in home directory
touch ~/.gitignore_global

Step 2: Configure Git to use it

git config --global core.excludesfile ~/.gitignore_global

Step 3: Add your global patterns

# Operating System Files
.DS_Store
Thumbs.db

# Editor Files
.vscode/
.idea/
*.swp

# Personal Notes
TODO.md
NOTES.md

Benefits:

💡 Best Practice: Use global .gitignore for personal/environment files, and project .gitignore for project-specific files (dependencies, build outputs, etc.).

9. Troubleshooting

Problem 1: .gitignore Not Working

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"

Problem 2: Need to Ignore Already Committed File

Steps:

  1. Add the file pattern to .gitignore
  2. Remove it from Git's tracking: git rm --cached filename
  3. Commit the changes

Problem 3: Want to Track File Inside Ignored Directory

Solution: Use negation pattern:

# Ignore entire logs directory
logs/

# But track important.log
!logs/important.log

Problem 4: Testing .gitignore Patterns

Command: Check if a file would be ignored:

git check-ignore -v filename

This shows which pattern in .gitignore is matching the file.

Problem 5: Wrong File Extension

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)

10. Best Practices

Do's:

Don'ts:

Sample Complete .gitignore

# ===========================
# 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/

Key Takeaways: