blob: 9bc1a06e2016d330fcd6b10e5cdda5366b82b6fc (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
# .gitignore 规范
## 基本规则
```
# 空行:不匹配任何文件
# 注释:以 # 开头
# 目录:以 / 结尾
# 取反:以 ! 开头表示不忽略
# 根目录:以 / 开头表示项目根目录
*.log # 忽略所有 .log 文件
node_modules/ # 忽略 node_modules 目录
/temp/ # 忽略根目录下的 temp 目录
**/.env # 忽略所有目录下的 .env 文件
!.gitkeep # 不忽略 .gitkeep 文件
```
## 通用 .gitignore
```
# ============================================
# 依赖目录
# ============================================
node_modules/
vendor/
# ============================================
# 构建产物
# ============================================
dist/
build/
target/
# ============================================
# 编辑器和 IDE
# ============================================
.idea/
.vscode/
*.sw?
# ============================================
# 环境配置
# ============================================
.env
.env.local
.env.*.local
# ============================================
# 日志文件
# ============================================
logs/
*.log
npm-debug.log*
# ============================================
# 系统文件
# ============================================
.DS_Store
Thumbs.db
# ============================================
# 缓存文件
# ============================================
.cache/
.eslintcache
.stylelintcache
```
## 项目特定配置
### 前端/文档项目补充
```
# VitePress
docs/.vitepress/dist
docs/.vitepress/cache
# Node.js
package-lock.json
yarn.lock
pnpm-lock.yaml
```
### 后端项目补充
```
# Maven
target/
pom.xml.tag
*.jar
!**/src/main/**/target/
# 敏感配置
application-local.yml
application-dev.yml
```
### Python 项目补充
```
# Python
__pycache__/
*.py[cod]
*$py.class
*.so
.Python
venv/
env/
ENV/
.pytest_cache/
# Jupyter Notebook
.ipynb_checkpoints
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype
.pytype/
```
### Go 项目补充
```
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool
*.out
# Dependency directories
vendor/
# Go workspace file
go.work
```
### Rust 项目补充
```
# Rust
/target/
**/*.rs.bk
*.pdb
Cargo.lock
```
## .gitignore 技巧
### 忽略文件但保留目录
```
logs/*
!logs/.gitkeep
```
### 检查忽略规则
```bash
git check-ignore -v filename
```
### 清理已提交的忽略文件
```bash
git rm --cached filename
git commit -m "chore: 移除不应提交的文件"
```
### 调试 .gitignore
```bash
# 查看文件是否被忽略以及被哪条规则匹配
git check-ignore -v path/to/file
# 查看所有被忽略的文件
git ls-files --others --ignored --exclude-standard
```
## 常见模式
### 忽略特定文件
```
# 忽略特定文件
config/local.json
secrets.yaml
```
### 忽略特定类型
```
# 忽略所有 .log 文件
*.log
# 忽略所有临时文件
*.tmp
*.temp
```
### 忽略目录
```
# 忽略所有 node_modules 目录
node_modules/
# 忽略根目录下的 build 目录
/build/
# 忽略任何位置的 build 目录
**/build/
```
### 取反规则
```
# 忽略所有 .a 文件
*.a
# 但不忽略 lib.a
!lib.a
# 忽略所有 TODO 文件
TODO*
# 但不忽略 TODO.md
!TODO.md
```
### 通配符
```
# * 匹配任意字符
*.log
# ** 匹配任意目录
**/temp/
# ? 匹配单个字符
file?.txt
# [] 匹配括号内任意字符
file[0-9].txt
```
## .gitignore 优先级
1. 命令行指定的文件(如 `git add -f`)
2. `.git/info/exclude`(本地排除规则)
3. `.gitignore`(项目级别,提交到仓库)
4. `~/.gitignore_global`(全局级别)
### 本地排除规则
对于不想提交到仓库的本地忽略规则:
```bash
# 编辑本地排除文件
git config --global core.excludesfile ~/.gitignore_global
# 或者使用 .git/info/exclude(仅当前仓库)
echo "secrets.yaml" >> .git/info/exclude
```
|