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_Store on macOS or Thumbs.db on 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

PatternWhat it Ignores
filename.txtA specific file named filename.txt
*.logAll files with the .log extension
build/The entire build folder and everything inside it
*.txtAll .txt files anywhere in the project
!important.txtException — do NOT ignore important.txt even if *.txt is ignored
# commentA comment line — Git ignores this line
**/logsAny folder named logs at any depth in the project
doc/*.txtAll .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.txt

If the file is ignored, the output shows which rule in .gitignore is causing it to be ignored:

.gitignore:3:*.txt    filename.txt

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

Leave a Comment

Your email address will not be published. Required fields are marked *