Skip to content

.gitignore

排除 files 从 Git 使用 .gitignore — 模式, 全局 ignores, and unignoring files.

你将学到

  • 如何 创建 and use .gitignore file
  • 常见 ignore 模式 for 不同 languages and 框架
  • Ignoring files globally 对比每-仓库
  • 如何 unignore previously ignored file

概念讲解

What .gitignore Does

A .gitignore file tells Git which files and directories to ignore — not track, not show in git 状态, and not include in git 添加 .. This keeps your repository clean and prevents accidentally committing sensitive or generated files.

# .gitignore
node_modules/
.env
*.log
dist/

Any file matching a pattern in .gitignore will not appear in git 状态 as untracked, and git 添加 . will skip them.

Common Patterns

# Ignore all .log files
*.log

# Ignore a specific file
secret.key

# Ignore an entire directory
node_modules/
dist/
build/

# Ignore everything in a directory but keep the directory
logs/*
!logs/.gitkeep

# Negate a pattern (un-ignore)
*.env
!.env.example

# Ignore all .md files except README.md
*.md
!README.md

# Ignore files only in the root directory
/TODO.txt

# Ignore files in a specific path
src/temp/*.js

Language-Specific Ignores

Every language and framework has files that should be ignored:

  • Node.js: node_modules/, dist/, .环境, npm-调试.log
  • Python: __pycache__/, *.pyc, .venv/, *.egg-信息/
  • Java: 目标/, *.类, .gradle/
  • macOS: .DS_Store
  • IDE: .vscode/, .idea/

Use the .gitignore Generator tool to create a tailored .gitignore for your project.

Global .gitignore

You can set a global ignore file for files you never want to commit on any project (like OS-specific files):

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

Then create ~/.gitignore_global:

# ~/.gitignore_global
.DS_Store
Thumbs.db
.vscode/

Already-Tracked Files

.gitignore only prevents untracked files from being added. If a file is already tracked (committed), adding it to .gitignore does NOT stop Git from tracking it. You must remove it first:

git rm --cached .env
echo ".环境" >> .gitignore
git commit -m "chore: 停止 tracking .环境 file"

--cached removes the file from Git but keeps it on your disk.

Where to Put .gitignore

You can place .gitignore files in the repository root or in any subdirectory. Rules in a subdirectory apply only to that directory and below. The root .gitignore is the most common location.

The Golden Rule

Never commit secrets, credentials, or large generated files. A proper .gitignore is your first line of defense. Always create it before your first git add ..

示例代码

              # Create a .gitignore for a Node.js project
cat > .gitignore << 'EOF'
# Dependencies
node_modules/

# Build output
dist/
build/

# Environment variables (NEVER commit these)
.env
.env.local
.env.*.local

# Logs
*.log
npm-debug.log*

# OS files
.DS_Store
Thumbs.db

# IDE
.vscode/
.idea/

# Test coverage
coverage/
EOF

# Verify it works — create files that should be ignored
mkdir -p node_modules dist
echo "secret" > .env
echo "log entry" > app.log

git status
# node_modules/, dist/, .env, app.log do NOT appear as untracked

# Force-add an ignored file (when you really need to)
git add -f .env.example

# Stop tracking a file that was already committed
git rm --cached .env
echo ".env" >> .gitignore
git commit -m "chore: stop tracking .env and add to gitignore"

# Set up a global gitignore for OS files
git config --global core.excludesfile ~/.gitignore_global
echo ".DS_Store" > ~/.gitignore_global

# Check which files are being ignored
git status --ignored
            

创建 comprehensive .gitignore for 节点.js 项目, 验证那个 ignored files 做不 appear in git 状态, force-添加异常, 停止 tracking already-committed file 使用 git rm --cached, 设置上 全局 ignore, and 检查 ignored files 使用 git 状态 --ignored.

动手试一试

常见错误

错误写法

添加 file 到 .gitignore 之后它 是 already committed

正确做法

.gitignore 仅 affects untracked files. 如果 file 是 already tracked, 添加它 到 .gitignore 做无. 运行 git rm --cached <file> 到停止 tracking 它 (file stays on disk), 然后添加它 到 .gitignore and 提交.

错误写法

Commiting .环境 files 使用 secrets and 然后只是删除它们

正确做法

删除 file in 新提交做 不移除它 从 history — secret 是 still in past 提交. Use git rm --cached 到停止 tracking, 然后 consider tools 如 git 筛选-仓库 or BFG 仓库-Cleaner 到 purge 它从 history. 总是 rotate leaked credentials immediately.

相关速查表

练习 .gitignore

5 个练习

练习 .gitignore

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误