Git 仓库
创建
git init
克隆
-
克隆所有记录:
git clone path/to/repository
-
克隆最新版本:
git clone --depth 1 path/to/repository
Git 工作流
文件的五个状态:原始(Normal)、修改(Modified/Created)、暂存(Staged)、提交(Submitted) 和远程推送(Pushed),这五个状态是逐步递进的过程,状态之间都可以进行向前或向后的状态切换。
撤销修改(Modified)
可退回一步,即文件从 修改 退回 原始 状态。注无法删除新建的文件,删除请用 clean
命令。
- 撤销单文件:
git checkout filepath
- 撤销全文件:
git checkout .
撤销创建(Created)
删除掉新建的文件
- 查看会删除的文件
git clean -n
- 删除未加入暂存区的新文件,但不删除 .gitignore 中指定的文件
git clean -f
- 删除内容包括新创建的文件夹,添加参数:
-d
- 删除指定路径的新文件
git clean -f filepath
- 交互的方式删除新文件
git clean -i
- 删除未加入暂存区的文件,包括 .gitignore 中指定的文件
git clean -xf
撤销暂存(Staged)
可退回两步,从 暂存 退回 修改 或 原始 状态。
- 退回至 修改:
- 撤销单文件:
git reset HEAD filepath
- 撤销全文件:
git reset HEAD
- 撤销单文件:
- 退回至 原始:
git reset --hard
撤销提交(Submitted)
可退回三步,从 提交 退回 暂存、修改 或 原始 状态。同时可设置撤销的次数:
- 撤销单次提交:
git reset --soft HEAD~1
- 撤销多次提交:
git reset --soft HEAD~n
撤销程度:
--mixed
:撤销至 修改 状态,默认参数;--soft
:撤销至 暂存 状态;--hard
:撤销至 原始 状态;
其他
修改提交注释
git commit --amend
强行 Push 覆盖远程分支
git push -f
分支管理
本地分支
- 显 示:
git branch
- 显示所有:
git branch -a
- 创 建:
git branch <branchname>
- 删 除:
git branch -d <branchname>
- 切 换:
git checkout <branchname>
- 修改名称:
git branch -m <oldBranchName> <newBranchName>
远程分支
-
推送本地已有分支到远程
git push origin <branchName>
-
拉取远程已有分支到本地
git fetch origin <remoteBranchName>` git checkout -b <localBranchName> origin/<remoteBranchName>
-
删除远程分支
git push --delete origin <remoteBranchName>
-
对比差异
git log master...origin/master
搁置修改
有时候修改了文件,但是临时需要切换到其他分支,此时不想暂存或者提交,可以选择搁置当前修改,将当前修改存储在隐藏区中。
注意,隐藏区的修改独立于分支,可从其他分支取出当前分支隐藏的修改
-
搁置修改:
git stash save "stashMsg"
-
查看搁置列表:
git list
-
还原修改:
git stash pop
或git stash apply <stashId>
Git Diff
分支间对比
git diff --stat master origin/master
Git Log
-
指定 Log 次数:
git log -<num>
,如:git log -2
-
单行模式
- 简略:
git log --oneline
- 完整:
git log --pretty=oneline
- 简略:
-
代码审查:
git log -p
-
统计行数变换:
git log --stat
显示
默认格式
--pretty=oneline
:单行显示,仅显示完整 Commit ID 与 Commit Message--pretty=short
:增加显示 Author 信息--pretty=full
:增加显示 Commit 用户信息--pretty=fuller
:增加显示时间相关信息
定制格式
标签
查询操作
-
列出标签
git tag
-
匹配模式列出标签
git tag -l <pattern>
- 支持前缀匹配:
git tag -l *_2020.01.01.01
- 支持后缀匹配:
git tag -l app_2020.*
- 支持同时匹配:
git tag -l *_2020.*
- 支持前缀匹配:
-
查看标签详细信息与提交信息
git show
-
校验标签信息,仅对附注标签有效
git tag -v <tagName>
-
按行查看提交备注信息
git tag -n <lineNumber> <tagName>
添加标签
轻量标签
将提交信息校验和存储到一个文件中,不保存任何其他信息,也就是说仅有标签名,没有其他任何信息。它的创建不得包含 -a
、-m
与 -s
选项
-
创建轻量标签
git tag <tagName>
-
查看轻量标签,可以看到,除了标注该提交上有 tag name,没有其他任何关于 tag 的信息
zohar@LAPTOP-FC5I09PK:~/zoharyips.github.io$ git show lig_weight_tag commit c819f38d0ded8411d0c427896530f83f10b3639c (HEAD -> master, tag: lig_weight_tag, origin/master, origin/HEAD) Author: zoharyips <zoharyips@outlook.com> Date: Wed Sep 2 18:39:20 2020 +0800 fix a bug diff --git a/_wiki/mysql-skills.md b/_wiki/mysql-skills.md index e67329f..6f00866 100644 --- a/_wiki/mysql-skills.md +++ b/_wiki/mysql-skills.md @@ -136,7 +136,9 @@ prism: true SELECT ifnull ((SELECT data FROM [table] GROUP BY data DESC LIMIT 1, 1), null) AS max; ```
附注标签
-
添加标签
git tag -a <tagName> -m <message>
-
推送标签
git push origin <tagName>
配置
查看配置
git config --list {--local | --global | --system}
local: 配置当前仓库
global: 当前用户所有仓库
system: 本系统的所有用户
彩色输出
git config color.ui true
单行显示
git config format.pretty oneline
取消换行符自动转换
git config --global core.autocrlf false
git config --global core.safecrlf true
用户配置
git config {--local | --global | --system} user.name 'username'
git congig {--local | --global | --system} user.email 'emailAddress'
git config {--local | --global | --system} credential.helper store # 保存用户名密码
git config --system --unset credential.helper # 清除用户名密码
编辑器
git config --global core.editor <editorName>