← 返回首页

.gitignore 忽略文件配置指南

什么是 .gitignore?

.gitignore 是 Git 版本控制系统中的一个配置文件,用于指定哪些文件或目录不应该被 Git 跟踪和提交到代码仓库中。

为什么需要 .gitignore?

在开发过程中,我们经常会产生一些不需要版本控制的文件:

基本语法

``gitignore

这是注释,以 # 开头

忽略所有 .log 文件

*.log

忽略 node_modules 目录

node_modules/

忽略所有 .tmp 文件,但不忽略 important.tmp

*.tmp !important.tmp

忽略 build 目录下的所有文件

build/

忽略所有以 temp 开头的文件

temp*

忽略 doc 目录下的 .txt 文件,但不忽略 doc/server/ 下的 .txt 文件

doc/*.txt

只忽略当前目录下的 config.ini,不忽略子目录下的 config.ini

/config.ini
`

常见项目的 .gitignore 模板

Python 项目

`gitignore

Byte-compiled / optimized / DLL files

__pycache__/ *.py[cod] *$py.class

Distribution / packaging

.Python build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ wheels/ *.egg-info/ .installed.cfg *.egg

Virtual environments

venv/ ENV/ env/

IDE

.idea/ .vscode/ *.swp *.swo

Testing

.tox/ .coverage htmlcov/ .pytest_cache/

Jupyter Notebook

.ipynb_checkpoints
`

Node.js 项目

`gitignore

Dependencies

node_modules/

Build output

dist/ build/

Logs

logs/ *.log npm-debug.log*

Environment variables

.env .env.local .env.*.local

IDE

.idea/ .vscode/ *.swp

OS

.DS_Store Thumbs.db

Testing

coverage/ .nyc_output/
`

Rust 项目

`gitignore

Build

target/ Cargo.lock

IDE

.idea/ .vscode/ *.iml

OS

.DS_Store Thumbs.db

Documentation

doc/
`

最佳实践

  1. 尽早创建: 在项目初始化时就创建 .gitignore 文件
  2. 团队统一: 确保团队成员使用相同的忽略规则
  3. 不要忽略重要文件: 小心使用通配符,避免误忽略
  4. 定期审查: 随着项目发展,更新 .gitignore 规则
  5. 使用模板: GitHub 提供了各种语言的 gitignore 模板

常用操作

`bash

检查哪些文件被忽略了

git check-ignore -v filename

强制添加被忽略的文件

git add -f filename

清除已跟踪但现在在 .gitignore 中的文件

git rm -r --cached . git add . git commit -m "更新 .gitignore"
`

总结

合理使用 .gitignore` 可以保持代码仓库的整洁,避免提交不必要的文件,提高团队协作效率。


最后更新: 2024-07-17