.gitignore
Git best practices
version control
code repository management
software development

Should you commit .gitignore into the Git repos?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Yes, you should almost always commit .gitignore into the repository. It is a project configuration file that ensures all contributors ignore the same files (build artifacts, dependencies, IDE settings, secrets). Without a shared .gitignore, each developer must manually configure their own ignore rules, leading to accidental commits of node_modules/, .env files, or OS-specific files like .DS_Store. The .gitignore file belongs in version control alongside your code. For personal ignore rules that should not affect the team, use .git/info/exclude or a global ~/.gitignore_global.

Why You Should Commit .gitignore

bash
1# A typical .gitignore for a Node.js project
2node_modules/
3dist/
4.env
5.env.local
6*.log
7coverage/
8.DS_Store
9Thumbs.db

When this file is committed, every developer who clones the repository automatically inherits these ignore rules. Without it:

  • New contributors accidentally commit node_modules/ (hundreds of MBs)
  • Secrets in .env files end up in the repository history permanently
  • OS-specific files (.DS_Store, Thumbs.db) clutter pull requests

What Belongs in .gitignore

bash
1# Build artifacts
2dist/
3build/
4*.o
5*.class
6*.pyc
7__pycache__/
8
9# Dependencies
10node_modules/
11vendor/
12.venv/
13
14# Environment and secrets
15.env
16.env.local
17.env.production
18*.pem
19credentials.json
20
21# IDE and editor files
22.idea/
23.vscode/
24*.swp
25*.swo
26
27# OS files
28.DS_Store
29Thumbs.db
30Desktop.ini
31
32# Test and coverage
33coverage/
34.nyc_output/
35htmlcov/
36
37# Logs
38*.log
39npm-debug.log*

What Does NOT Belong in .gitignore

bash
1# DO commit these — they are project configuration
2package.json
3package-lock.json     # Lock file ensures reproducible installs
4yarn.lock
5Pipfile.lock
6Gemfile.lock
7.gitignore            # The ignore file itself
8
9# DO NOT put these in project .gitignore — they are personal
10# Use ~/.gitignore_global instead:
11# .idea/              # Not everyone uses IntelliJ
12# .vscode/            # Not everyone uses VS Code
13# *.swp               # Not everyone uses Vim

IDE-specific files are debatable. If the entire team uses the same IDE, including .idea/ or .vscode/ settings in the project .gitignore is reasonable. If the team uses mixed editors, use personal global ignores.

Personal Ignores: .git/info/exclude

bash
1# .git/info/exclude — per-repo personal ignores (not committed)
2# This file already exists in every Git repo
3
4# Add your personal patterns here
5my-local-notes.txt
6scratch/
7.env.mylocal

.git/info/exclude works exactly like .gitignore but is not tracked by Git. Use it for files that are specific to your local setup and should not affect other contributors.

Global Ignores: ~/.gitignore_global

bash
1# Set up a global gitignore
2git config --global core.excludesfile ~/.gitignore_global
3
4# ~/.gitignore_global — applies to ALL repositories
5.DS_Store
6Thumbs.db
7*.swp
8*.swo
9*~
10.idea/
11.vscode/settings.json

A global gitignore handles OS and editor files across all repositories. This keeps the project .gitignore focused on project-specific patterns.

Creating .gitignore for New Projects

bash
1# GitHub provides templates for common languages/frameworks
2# Use gitignore.io to generate comprehensive .gitignore files
3curl -sL https://www.toptal.com/developers/gitignore/api/node,python,macos > .gitignore
4
5# Or use GitHub's template when creating a repository
6# github.com/new -> "Add .gitignore" dropdown
7
8# Or copy from github/gitignore repository
9curl -sL https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore > .gitignore
bash
# Stage and commit .gitignore
git add .gitignore
git commit -m "Add .gitignore"

Fixing Already-Tracked Files

bash
1# If you added .gitignore after files were already committed,
2# the ignore rules won't apply to tracked files
3
4# Remove files from tracking (keeps them on disk)
5git rm --cached node_modules/ -r
6git rm --cached .env
7git rm --cached .DS_Store
8
9# Commit the removal
10git commit -m "Remove tracked files that should be ignored"
11
12# Now .gitignore rules take effect for these files
13
14# Nuclear option: remove all cached files and re-add
15git rm -r --cached .
16git add .
17git commit -m "Apply .gitignore to all files"

.gitignore only affects untracked files. Files already committed must be explicitly removed from tracking with git rm --cached before the ignore rules apply.

Multiple .gitignore Files

 
1project/
2├── .gitignore          # Root: node_modules/, dist/, .env
3├── src/
4│   └── .gitignore      # src-specific: *.generated.ts
5├── docs/
6│   └── .gitignore      # docs-specific: _site/, .sass-cache/
7└── tests/
8    └── .gitignore      # tests-specific: fixtures/large-files/

Git supports .gitignore files in any directory. Rules in subdirectory .gitignore files apply only to that directory and its children. This is useful for monorepos where different subdirectories have different ignore requirements.

Common Pitfalls

  • Not committing .gitignore: Without a committed .gitignore, each contributor must set up their own ignore rules. New contributors will accidentally commit generated files, dependencies, and secrets that pollute the repository.
  • Adding .gitignore after files are tracked: .gitignore does not retroactively untrack files. If node_modules/ was committed before the ignore rule was added, it stays tracked. Run git rm --cached -r node_modules/ to untrack it.
  • Putting IDE-specific rules in the project .gitignore: Adding .idea/ or .vscode/ to the project .gitignore assumes everyone uses the same IDE. Use ~/.gitignore_global for personal editor files so the project .gitignore stays editor-neutral.
  • Ignoring lock files: package-lock.json, yarn.lock, and Pipfile.lock should NOT be in .gitignore. They ensure reproducible dependency installations across environments. Ignoring them causes "works on my machine" bugs.
  • Committing secrets then adding to .gitignore: Adding .env to .gitignore after it was already committed does not remove it from Git history. The secrets remain in past commits. Use git filter-branch or BFG Repo-Cleaner to purge them, then rotate all exposed credentials.

Summary

  • Always commit .gitignore — it is a shared project configuration file
  • Include build artifacts, dependencies, environment files, and OS-specific files
  • Use .git/info/exclude for per-repo personal ignores that should not be committed
  • Use ~/.gitignore_global for IDE and OS files that apply across all repositories
  • Run git rm --cached to untrack files that were committed before adding ignore rules
  • Use gitignore.io or GitHub templates to generate comprehensive .gitignore files for your stack

Course illustration
Course illustration

All Rights Reserved.