The response has been limited to 50k tokens of the smallest files in the repo. You can remove this limitation by removing the max tokens filter.
├── .gitignore
├── README.md
└── assets
    ├── git.png
    └── gitcz.png


/.gitignore:
--------------------------------------------------------------------------------
1 | # osx
2 | .DS_Store
3 | 
4 | # vscode
5 | .vscode
6 | 


--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
  1 | # Git 的奇技淫巧:see_no_evil:
  2 | 
  3 | > Git 常用命令集合,Fork 自 [tips](https://github.com/git-tips/tips) 项目
  4 | 
  5 | Git 是一个分布式版本管理工具,版本管理工具就是大家在写东西的时候都用过 **回撤**这个功能,但是回撤只能回撤几步,假如想要找回我三天之前的修改,光用回撤是找不回来的。而**版本管理工具能记录每次的修改**,只要提交到版本仓库,就可以找到之前任何时刻的状态(文本状态)。
  6 | 
  7 | 下面的内容就是列举了常用的 Git 命令和一些小技巧,可以通过页面内查找的方式 `Ctrl/Command+f` 进行快速查找。
  8 | 
  9 | ## 开卷必读
 10 | 
 11 | *如果之前未使用过 Git,可以学习 [Git 小白教程](https://rogerdudler.github.io/git-guide/index.zh.html)入门*
 12 | 
 13 | 1. **一定要先测试命令的效果后**,再用于工作环境中,以防造成不能弥补的后果!**到时候别拿着砍刀来找我**
 14 | 2. 所有的命令都在 `git version 2.7.4 (Apple Git-66)` 下测试通过
 15 | 3. 统一概念:
 16 | 	- 工作区:改动(增删文件和内容)
 17 | 	- 暂存区:输入命令:`git add 改动的文件名`,此次改动就放到了 ‘暂存区’
 18 | 	- 本地仓库(简称:本地):输入命令:`git commit 此次修改的描述`,此次改动就放到了本地仓库,每个 commit,我叫它为一个版本。
 19 | 	- 远程仓库(简称:远程):输入命令:`git push 远程仓库`,此次改动就放到了远程仓库(GitHub 等)
 20 | 	- commit-id:输出命令:`git log`,最上面那行 `commit xxxxxx`,后面的字符串就是 commit-id
 21 | 4. 如果喜欢这个项目,欢迎 Star、提交 Pr、[反馈问题](https://github.com/521xueweihan/git-tips/issues)😊
 22 | 
 23 | ## 目录
 24 | * [脑图](#脑图)
 25 | * [展示帮助信息](#展示帮助信息)
 26 | * [回到远程仓库的状态](#回到远程仓库的状态)
 27 | * [重设第一个commit](#重设第一个-commit)
 28 | * [查看冲突文件列表](#查看冲突文件列表)
 29 | * [展示工作区和暂存区的不同](#展示工作区和暂存区的不同)
 30 | * [展示暂存区和最近版本的不同](#展示暂存区和最近版本的不同)
 31 | * [展示暂存区、工作区和最近版本的不同](#展示暂存区工作区和最近版本的不同)
 32 | * [快速切换到上一个分支](#快速切换到上一个分支)
 33 | * [删除已经合并到 master 的分支](#删除已经合并到-master-的分支)
 34 | * [展示本地分支关联远程仓库的情况](#展示本地分支关联远程仓库的情况)
 35 | * [关联远程分支](#关联远程分支)
 36 | * [列出所有远程分支](#列出所有远程分支)
 37 | * [列出本地和远程分支](#列出本地和远程分支)
 38 | * [查看远程分支和本地分支的对应关系](#查看远程分支和本地分支的对应关系)
 39 | * [远程删除了分支本地也想删除](#远程删除了分支本地也想删除)
 40 | * [创建并切换到本地分支](#创建并切换到本地分支)
 41 | * [从远程分支中创建并切换到本地分支](#从远程分支中创建并切换到本地分支)
 42 | * [删除本地分支](#删除本地分支)
 43 | * [删除远程分支](#删除远程分支)
 44 | * [重命名本地分支](#重命名本地分支)
 45 | * [查看标签](#查看标签)
 46 | * [查看标签详细信息](#查看标签详细信息)
 47 | * [本地创建标签](#本地创建标签)
 48 | * [推送标签到远程仓库](#推送标签到远程仓库)
 49 | * [删除本地标签](#删除本地标签)
 50 | * [删除远程标签](#删除远程标签)
 51 | * [切回到某个标签](#切回到某个标签)
 52 | * [放弃工作区的修改](#放弃工作区的修改)
 53 | * [恢复删除的文件](#恢复删除的文件)
 54 | * [以新增一个 commit 的方式还原某一个 commit 的修改](#以新增一个-commit-的方式还原某一个-commit-的修改)
 55 | * [回到某个 commit 的状态,并删除后面的 commit](#回到某个-commit-的状态并删除后面的-commit)
 56 | * [修改上一个 commit 的描述](#修改上一个-commit-的描述)
 57 | * [查看 commit 历史](#查看-commit-历史)
 58 | * [显示本地更新过 HEAD 的 git 命令记录](#显示本地更新过-head-的-git-命令记录)
 59 | * [修改作者名](#修改作者名)
 60 | * [修改远程仓库的 url](#修改远程仓库的-url)
 61 | * [增加远程仓库](#增加远程仓库)
 62 | * [列出所有远程仓库](#列出所有远程仓库)
 63 | * [查看两个星期内的改动](#查看两个星期内的改动)
 64 | * [把 A 分支的某一个 commit,放到 B 分支上](#把-A-分支的某一个-commit放到-B-分支上)
 65 | * [给 git 命令起别名](#给-git-命令起别名)
 66 | * [存储当前的修改,但不用提交 commit](#存储当前的修改但不用提交-commit)
 67 | * [保存当前状态,包括 untracked 的文件](#保存当前状态包括-untracked-的文件)
 68 | * [展示所有 stashes](#展示所有-stashes)
 69 | * [回到某个 stash 的状态](#回到某个-stash-的状态)
 70 | * [回到最后一个 stash 的状态,并删除这个 stash](#回到最后一个-stash-的状态并删除这个-stash)
 71 | * [删除所有的 stash](#删除所有的-stash)
 72 | * [从 stash 中拿出某个文件的修改](#从-stash-中拿出某个文件的修改)
 73 | * [展示所有 tracked 的文件](#展示所有-tracked-的文件)
 74 | * [展示所有 untracked 的文件](#展示所有-untracked-的文件)
 75 | * [展示所有忽略的文件](#展示所有忽略的文件)
 76 | * [强制删除 untracked 的文件](#强制删除-untracked-的文件)
 77 | * [强制删除 untracked 的目录](#强制删除-untracked-的目录)
 78 | * [展示简化的 commit 历史](#展示简化的-commit-历史)
 79 | * [查看某段代码是谁写的](#查看某段代码是谁写的)
 80 | * [把某一个分支导出成一个文件](#把某一个分支导出成一个文件)
 81 | * [从包中导入分支](#从包中导入分支)
 82 | * [执行 rebase 之前自动 stash](#执行-rebase-之前自动-stash)
 83 | * [从远程仓库根据 ID,拉下某一状态,到本地分支](#从远程仓库根据-ID-拉下某一状态-到本地分支)
 84 | * [详细展示一行中的修改](#详细展示一行中的修改)
 85 | * [清除 `.gitignore` 文件中记录的文件](#清除-gitignore-文件中记录的文件)
 86 | * [展示所有 alias 和 configs](#展示所有-alias-和-configs)
 87 | * [展示忽略的文件](#展示忽略的文件)
 88 | * [commit 历史中显示 Branch1 有的,但是 Branch2 没有 commit](#commit-历史中显示-Branch1-有的但是-Branch2-没有-commit)
 89 | * [在 commit log 中显示 GPG 签名](#在-commit-log-中显示-GPG-签名)
 90 | * [删除全局设置](#删除全局设置)
 91 | * [新建并切换到新分支上,同时这个分支没有任何 commit](#新建并切换到新分支上同时这个分支没有任何-commit)
 92 | * [展示任意分支某一文件的内容](#展示任意分支某一文件的内容)
 93 | * [clone 下来指定的单一分支](#clone-下来指定的单一分支)
 94 | * [clone 最新一次提交](#clone-最新一次提交)
 95 | * [忽略某个文件的改动](#忽略某个文件的改动)
 96 | * [忽略文件的权限变化](#忽略文件的权限变化)
 97 | * [以最后提交的顺序列出所有 Git 分支](#以最后提交的顺序列出所有-Git-分支)
 98 | * [在 commit log 中查找相关内容](#在-commit-log-中查找相关内容)
 99 | * [把暂存区的指定 file 放到工作区中](#把暂存区的指定-file-放到工作区中)
100 | * [强制推送](#强制推送)
101 | * [git 配置 http 和 socks 代理](#git-配置-http-和-socks-代理)
102 | * [git 配置 ssh 代理](#git-配置-ssh-代理)
103 | * [优雅的Commit信息](#优雅的Commit信息)
104 | * [commit工具](#commit工具)
105 | * [声明](#声明)
106 | 
107 | ## 展示帮助信息
108 | ```sh
109 | git help -g
110 | ```
111 | The command output as below:
112 | 
113 | ```
114 | The common Git guides are:
115 |    attributes          Defining attributes per path
116 |    cli                 Git command-line interface and conventions
117 |    core-tutorial       A Git core tutorial for developers
118 |    cvs-migration       Git for CVS users
119 |    diffcore            Tweaking diff output
120 |    everyday            A useful minimum set of commands for Everyday Git
121 |    glossary            A Git Glossary
122 |    hooks               Hooks used by Git
123 |    ignore              Specifies intentionally untracked files to ignore
124 |    modules             Defining submodule properties
125 |    namespaces          Git namespaces
126 |    repository-layout    Git Repository Layout
127 |    revisions           Specifying revisions and ranges for Git
128 |    tutorial            A tutorial introduction to Git
129 |    tutorial-2          A tutorial introduction to Git: part two
130 |    workflows           An overview of recommended workflows with Git
131 | 
132 | 'git help -a' and 'git help -g' list available subcommands and some concept guides. See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.
133 | ```
134 | 
135 | ## 回到远程仓库的状态
136 | 
137 | 抛弃本地所有的修改,回到远程仓库的状态。
138 | ```sh
139 | git fetch --all && git reset --hard origin/master
140 | ```
141 | 
142 | ## 重设第一个 commit
143 | 
144 | 也就是把所有的改动都重新放回工作区,并**清空所有的 commit**,这样就可以重新提交第一个 commit 了
145 | 
146 | ```sh
147 | git update-ref -d HEAD
148 | ```
149 | 
150 | ## 查看冲突文件列表
151 | 
152 | 展示工作区的冲突文件列表
153 | ```sh
154 | git diff --name-only --diff-filter=U
155 | ```
156 | ## 展示工作区和暂存区的不同
157 | 
158 | 输出**工作区**和**暂存区**的 different (不同)。
159 | 
160 | ```sh
161 | git diff
162 | ```
163 | 
164 | 还可以展示本地仓库中任意两个 commit 之间的文件变动:
165 | ```sh
166 | git diff <commit-id> <commit-id>
167 | ```
168 | 
169 | ## 展示暂存区和最近版本的不同
170 | 
171 | 输出**暂存区**和本地最近的版本 (commit) 的 different (不同)。
172 | ```sh
173 | git diff --cached
174 | ```
175 | 
176 | ## 展示暂存区、工作区和最近版本的不同
177 | 
178 | 输出**工作区**、**暂存区** 和本地最近的版本 (commit) 的 different (不同)。
179 | 
180 | ```sh
181 | git diff HEAD
182 | ```
183 | 
184 | ## 快速切换到上一个分支
185 | 
186 | ```sh
187 | git checkout -
188 | ```
189 | 
190 | ## 删除已经合并到 master 的分支
191 | 
192 | ```sh
193 | git branch --merged master | grep -v '^\*\|  master' | xargs -n 1 git branch -d
194 | ```
195 | 
196 | ## 展示本地分支关联远程仓库的情况
197 | ```sh
198 | git branch -vv
199 | ```
200 | 
201 | ## 关联远程分支
202 | 
203 | 关联之后,`git branch -vv` 就可以展示关联的远程分支名了,同时推送到远程仓库直接:`git push`,不需要指定远程仓库了。
204 | ```sh
205 | git branch -u origin/mybranch
206 | ```
207 | 
208 | 或者在 push 时加上 `-u` 参数
209 | ```sh
210 | git push origin/mybranch -u
211 | ```
212 | 
213 | ## 列出所有远程分支
214 | 
215 | -r 参数相当于:remote
216 | ```sh
217 | git branch -r
218 | ```
219 | 
220 | ## 列出本地和远程分支
221 | 
222 | -a 参数相当于:all
223 | ```sh
224 | git branch -a
225 | ```
226 | 
227 | ## 查看远程分支和本地分支的对应关系
228 | 
229 | ```sh
230 | git remote show origin
231 | ```
232 | 
233 | ## 远程删除了分支本地也想删除
234 | 
235 | ```sh
236 | git remote prune origin
237 | ```
238 | 
239 | ## 创建并切换到本地分支
240 | ```sh
241 | git checkout -b <branch-name>
242 | ```
243 | 
244 | ## 从远程分支中创建并切换到本地分支
245 | 
246 | ```sh
247 | git checkout -b <branch-name> origin/<branch-name>
248 | ```
249 | 
250 | ## 删除本地分支
251 | 
252 | ```sh
253 | git branch -d <local-branchname>
254 | ```
255 | 
256 | ## 删除远程分支
257 | 
258 | ```sh
259 | git push origin --delete <remote-branchname>
260 | ```
261 | 
262 | 或者
263 | 
264 | ```sh
265 | git push origin :<remote-branchname>
266 | ```
267 | 
268 | ## 重命名本地分支
269 | 
270 | ```sh
271 | git branch -m <new-branch-name>
272 | ```
273 | 
274 | ## 查看标签
275 | 
276 | ```sh
277 | git tag
278 | ```
279 | 展示当前分支的最近的 tag
280 | 
281 | ```sh
282 | git describe --tags --abbrev=0
283 | ```
284 | 
285 | ## 查看标签详细信息
286 | 
287 | ```sh
288 | git tag -ln
289 | ```
290 | 
291 | ## 本地创建标签
292 | 
293 | ```sh
294 | git tag <version-number>
295 | ```
296 | 
297 | 默认 tag 是打在最近的一次 commit 上,如果需要指定 commit 打 tag:
298 | ```sh
299 | $ git tag -a <version-number> -m "v1.0 发布(描述)" <commit-id>
300 | ```
301 | 
302 | ## 推送标签到远程仓库
303 | 
304 | 首先要保证本地创建好了标签才可以推送标签到远程仓库:
305 | 
306 | ```sh
307 | git push origin <local-version-number>
308 | ```
309 | 
310 | 一次性推送所有标签,同步到远程仓库:
311 | 
312 | ```sh
313 | git push origin --tags
314 | ```
315 | 
316 | ## 删除本地标签
317 | 
318 | ```sh
319 | git tag -d <tag-name>
320 | ```
321 | 
322 | ## 删除远程标签
323 | 
324 | ```sh
325 | git push origin --delete tag <tagname>
326 | ```
327 | 
328 | ## 切回到某个标签
329 | 
330 | 一般上线之前都会打 tag,就是为了防止上线后出现问题,方便快速回退到上一版本。下面的命令是回到某一标签下的状态:
331 | ```sh
332 | git checkout -b branch_name tag_name
333 | ```
334 | 
335 | ## 放弃工作区的修改
336 | ```sh
337 | git checkout <file-name>
338 | ```
339 | 
340 | 放弃所有修改:
341 | ```sh
342 | git checkout .
343 | ```
344 | 
345 | ## 恢复删除的文件
346 | ```sh
347 | git rev-list -n 1 HEAD -- <file_path> #得到 deleting_commit
348 | 
349 | git checkout <deleting_commit>^ -- <file_path> #回到删除文件 deleting_commit 之前的状态
350 | ```
351 | 
352 | ## 以新增一个 commit 的方式还原某一个 commit 的修改
353 | 
354 | ```sh
355 | git revert <commit-id>
356 | ```
357 | 
358 | ## 回到某个 commit 的状态,并删除后面的 commit
359 | 
360 | 和 revert 的区别:reset 命令会抹去某个 commit id 之后的所有 commit
361 | 
362 | ```sh
363 | git reset <commit-id>  #默认就是-mixed参数。
364 | 
365 | git reset --mixed HEAD^  #回退至上个版本,它将重置HEAD到另外一个commit,并且重置暂存区以便和HEAD相匹配,但是也到此为止。工作区不会被更改。
366 | 
367 | git reset --soft HEAD~3  #回退至三个版本之前,只回退了commit的信息,暂存区和工作区与回退之前保持一致。如果还要提交,直接commit即可  
368 | 
369 | git reset --hard <commit-id>  #彻底回退到指定commit-id的状态,暂存区和工作区也会变为指定commit-id版本的内容
370 | ```
371 | 
372 | ## 修改上一个 commit 的描述
373 | 
374 | 如果暂存区有改动,同时也会将暂存区的改动提交到上一个 commit
375 | 
376 | ```sh
377 | git commit --amend
378 | ```
379 | 
380 | ## 查看 commit 历史
381 | ```sh
382 | git log
383 | ```
384 | 
385 | ## 查看某段代码是谁写的
386 | 
387 | blame 的意思为‘责怪’,你懂的。
388 | 
389 | ```sh
390 | git blame <file-name>
391 | ```
392 | 
393 | ## 显示本地更新过 HEAD 的 git 命令记录
394 | 
395 | 每次更新了 HEAD 的 git 命令比如 commit、amend、cherry-pick、reset、revert 等都会被记录下来(不限分支),就像 shell 的 history 一样。
396 | 这样你可以 reset 到任何一次更新了 HEAD 的操作之后,而不仅仅是回到当前分支下的某个 commit 之后的状态。
397 | 
398 | ```sh
399 | git reflog
400 | ```
401 | 
402 | ## 修改作者名
403 | 
404 | ```sh
405 | git commit --amend --author='Author Name <email@address.com>'
406 | ```
407 | 
408 | ## 修改远程仓库的 url
409 | 
410 | ```sh
411 | git remote set-url origin <URL>
412 | ```
413 | 
414 | ## 增加远程仓库
415 | 
416 | ```sh
417 | git remote add origin <remote-url>
418 | ```
419 | 
420 | ## 列出所有远程仓库
421 | 
422 | ```sh
423 | git remote
424 | ```
425 | 
426 | ## 查看两个星期内的改动
427 | ```sh
428 | git whatchanged --since='2 weeks ago'
429 | ```
430 | 
431 | ## 把 A 分支的某一个 commit,放到 B 分支上
432 | 
433 | 这个过程需要 `cherry-pick` 命令,[参考](http://sg552.iteye.com/blog/1300713#bc2367928)
434 | 
435 | ```sh
436 | git checkout <branch-name> && git cherry-pick <commit-id>
437 | ```
438 | 
439 | ## 给 git 命令起别名
440 | 
441 | 简化命令
442 | 
443 | ```sh
444 | git config --global alias.<handle> <command>
445 | 
446 | 比如:git status 改成 git st,这样可以简化命令
447 | 
448 | git config --global alias.st status
449 | ```
450 | 
451 | ## 存储当前的修改,但不用提交 commit
452 | 
453 | 详解可以参考[廖雪峰老师的 git 教程](http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137602359178794d966923e5c4134bc8bf98dfb03aea3000)
454 | ```sh
455 | git stash
456 | ```
457 | 
458 | ## 保存当前状态,包括 untracked 的文件
459 | 
460 | untracked 文件:新建的文件
461 | ```sh
462 | git stash -u
463 | ```
464 | 
465 | ## 展示所有 stashes
466 | ```sh
467 | git stash list
468 | ```
469 | 
470 | ## 回到某个 stash 的状态
471 | ```sh
472 | git stash apply <stash@{n}>
473 | ```
474 | 
475 | ## 回到最后一个 stash 的状态,并删除这个 stash
476 | 
477 | ```sh
478 | git stash pop
479 | ```
480 | 
481 | ## 删除所有的 stash
482 | 
483 | ```sh
484 | git stash clear
485 | ```
486 | 
487 | ## 从 stash 中拿出某个文件的修改
488 | ```sh
489 | git checkout <stash@{n}> -- <file-path>
490 | ```
491 | 
492 | ## 展示所有 tracked 的文件
493 | ```sh
494 | git ls-files -t
495 | ```
496 | 
497 | ## 展示所有 untracked 的文件
498 | ```sh
499 | git ls-files --others
500 | ```
501 | 
502 | ## 展示所有忽略的文件
503 | 
504 | ```sh
505 | git ls-files --others -i --exclude-standard
506 | ```
507 | 
508 | ## 强制删除 untracked 的文件
509 | 
510 | 可以用来删除新建的文件。如果不指定文件文件名,则清空所有工作的 untracked 文件。`clean` 命令,**注意两点**:
511 | 1. clean 后,删除的文件无法找回
512 | 2. 不会影响 tracked 的文件的改动,只会删除 untracked 的文件
513 | 
514 | ```sh
515 | git clean <file-name> -f
516 | ```
517 | 
518 | ## 强制删除 untracked 的目录
519 | 
520 | 可以用来删除新建的目录,**注意**:这个命令也可以用来删除 untracked 的文件。详情见上一条
521 | 
522 | ```sh
523 | git clean <directory-name> -df
524 | ```
525 | 
526 | ## 展示简化的 commit 历史
527 | ```sh
528 | git log --pretty=oneline --graph --decorate --all
529 | ```
530 | 
531 | ## 把某一个分支导出成一个文件
532 | ```sh
533 | git bundle create <file> <branch-name>
534 | ```
535 | 
536 | ## 从包中导入分支
537 | 
538 | 新建一个分支,分支内容就是上面 `git bundle create` 命令导出的内容
539 | 
540 | ```sh
541 | git clone repo.bundle <repo-dir> -b <branch-name>
542 | ```
543 | 
544 | ## 执行 rebase 之前自动 stash
545 | 
546 | ```sh
547 | git rebase --autostash
548 | ```
549 | 
550 | ## 从远程仓库根据 ID,拉下某一状态,到本地分支
551 | 
552 | ```sh
553 | git fetch origin pull/<id>/head:<branch-name>
554 | ```
555 | 
556 | ## 详细展示一行中的修改
557 | 
558 | ```sh
559 | git diff --word-diff
560 | ```
561 | 
562 | ## 清除 gitignore 文件中记录的文件
563 | 
564 | ```sh
565 | git clean -X -f
566 | ```
567 | 
568 | ## 展示所有 alias 和 configs
569 | 
570 | **注意:** config 分为:当前目录(local)和全局(golbal)的 config,默认为当前目录的 config
571 | 
572 | ```sh
573 | git config --local --list (当前目录)
574 | git config --global --list (全局)
575 | ```
576 | 
577 | ## 展示忽略的文件
578 | ```sh
579 | git status --ignored
580 | ```
581 | 
582 | ## commit 历史中显示 Branch1 有的,但是 Branch2 没有 commit
583 | ```sh
584 | git log Branch1 ^Branch2
585 | ```
586 | 
587 | ## 在 commit log 中显示 GPG 签名
588 | ```sh
589 | git log --show-signature
590 | ```
591 | 
592 | ## 删除全局设置
593 | 
594 | ```sh
595 | git config --global --unset <entry-name>
596 | ```
597 | 
598 | ## 新建并切换到新分支上,同时这个分支没有任何 commit
599 | 
600 | 相当于保存修改,但是重写 commit 历史
601 | 
602 | ```sh
603 | git checkout --orphan <branch-name>
604 | ```
605 | 
606 | ## 展示任意分支某一文件的内容
607 | 
608 | ```sh
609 | git show <branch-name>:<file-name>
610 | ```
611 | 
612 | ## clone 下来指定的单一分支
613 | ```sh
614 | git clone -b <branch-name> --single-branch https://github.com/user/repo.git
615 | ```
616 | 
617 | ## clone 最新一次提交
618 | 
619 | 只会 clone 最近一次提交,将减少 clone 时间
620 | 
621 | ```sh
622 | git clone --depth=1 https://github.com/user/repo.git
623 | ```
624 | 
625 | ## 忽略某个文件的改动
626 | 
627 | 关闭 track 指定文件的改动,也就是 Git 将不会在记录这个文件的改动
628 | 
629 | ```sh
630 | git update-index --assume-unchanged path/to/file
631 | ```
632 | 
633 | 恢复 track 指定文件的改动
634 | 
635 | ```sh
636 | git update-index --no-assume-unchanged path/to/file
637 | ```
638 | 
639 | ## 忽略文件的权限变化
640 | 
641 | 不再将文件的权限变化视作改动
642 | 
643 | ```sh
644 | git config core.fileMode false
645 | ```
646 | 
647 | ## 以最后提交的顺序列出所有 Git 分支
648 | 
649 | 最新的放在最上面
650 | 
651 | ```sh
652 | git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads/
653 | ```
654 | 
655 | ## 在 commit log 中查找相关内容
656 | 
657 | 通过 grep 查找,given-text:所需要查找的字段
658 | 
659 | 
660 | ```sh
661 | git log --all --grep='<given-text>'
662 | ```
663 | 
664 | ## 把暂存区的指定 file 放到工作区中
665 | 
666 | 不添加参数,默认是 `-mixed`
667 | 
668 | ```sh
669 | git reset <file-name>
670 | ```
671 | 
672 | ## 强制推送
673 | 
674 | ```sh
675 | git push -f <remote-name> <branch-name>
676 | ```
677 | 
678 | ## git 配置 http 和 socks 代理
679 | 
680 | ```sh
681 | git config --global https.proxy 'http://127.0.0.1:8001'   # 适用于 privoxy 将 socks 协议转为 http 协议的 http 端口
682 | git config --global http.proxy 'http://127.0.0.1:8001'
683 | git config --global socks.proxy "127.0.0.1:1080"
684 | ```
685 | 
686 | ## git 配置 ssh 代理
687 | 
688 | ```sh
689 | $ cat ~/.ssh/config
690 | Host gitlab.com
691 | ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p    # 直接使用 shadowsocks 提供的 socks5 代理端口
692 | 
693 | Host github.com
694 | ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p    
695 | ```
696 | 
697 | 
698 | ## 脑图
699 | 
700 | ![](./assets/git.png)
701 | 
702 | ## 优雅的Commit信息
703 | 
704 | 使用[Angular团队提交规范](https://github.com/angular/angular.js/blob/master/DEVELOPERS.md#-git-commit-guidelines)
705 | 
706 | 主要有以下组成
707 | 
708 | * 标题行: 必填, 描述主要修改类型和内容
709 | * 主题内容: 描述为什么修改, 做了什么样的修改, 以及开发的思路等等
710 | * 页脚注释: 放 Breaking Changes 或 Closed Issues
711 | 
712 | 常用的修改项
713 | 
714 | * type: commit 的类型
715 | * feat: 新特性
716 | * fix: 修改问题
717 | * refactor: 代码重构
718 | * docs: 文档修改
719 | * style: 代码格式修改, 注意不是 css 修改
720 | * test: 测试用例修改
721 | * chore: 其他修改, 比如构建流程, 依赖管理.
722 | * scope: commit 影响的范围, 比如: route, component, utils, build...
723 | * subject: commit 的概述
724 | * body: commit 具体修改内容, 可以分为多行
725 | * footer: 一些备注, 通常是 BREAKING CHANGE 或修复的 bug 的链接.
726 | 
727 | ## commit工具
728 | 
729 | 可以使用[cz-cli](https://github.com/commitizen/cz-cli)工具代替 `git commit`
730 | 
731 | 全局安装
732 | 
733 | ```shell
734 | npm install -g commitizen cz-conventional-changelog
735 | 
736 | echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
737 | ```
738 | 全局安装后使用 `git cz` 代替 `git commit`就可以了,如下图
739 | 
740 | ![](./assets/gitcz.png)
741 | 
742 | 
743 | ## 声明
744 | <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh"><img alt="知识共享许可协议" style="border-width: 0" src="https://licensebuttons.net/l/by-nc-nd/4.0/88x31.png"></a><br>本作品采用 <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/deed.zh">署名-非商业性使用-禁止演绎 4.0 国际</a> 进行许可。
745 | 
746 | **[⬆ 返回顶部](#目录)**
747 | 


--------------------------------------------------------------------------------
/assets/git.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/521xueweihan/git-tips/a38f75e1018f30a1877fae320c735aa69d9d5695/assets/git.png


--------------------------------------------------------------------------------
/assets/gitcz.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/521xueweihan/git-tips/a38f75e1018f30a1877fae320c735aa69d9d5695/assets/gitcz.png


--------------------------------------------------------------------------------