-
主干分支
-
稳定分支
-
开发分支
-
补丁分支
-
修改分支
-
创建分支
-
添加提交
-
提交 PR 请求
-
讨论和评估代码
-
部署检测
-
合并代码
-
带生产分支
-
带环境分支
-
带发布分支
-
使用命令行来操作,简洁且效率高
-
区分 subject 和 body 内容,使用空行隔开
-
subject 一般不超过 50 个字符
-
body 每一行的长度控制在 72 个字符
-
subject 结尾不需要使用句号或者点号结尾
-
body 用来详细解释此次提交具体做了什么
-
可使用模板文件,然后根据项目实际进行修改
-
不要直接在主干分支上面进行开发
-
在新建的分支上进行功能的开发和问题的修复
-
使用 release 分支发布代码和版本维护(release/1.32)
-
使用 tag 来标记版本(A-大 feature 功能;B-小 feature 功能;C-只修 bug)
# 工作区 -> 暂存区
$ git add <file/dir>
# 暂存区 -> 本地仓库
$ git commit -m "some info"
# 本地仓库 -> 远程仓库
$ git push origin master # 本地 master 分支推送到远程 origin 仓库
# 工作区 <- 暂存区
$ git checkout -- <file> # 暂存区文件内容覆盖工作区文件内容
# 暂存区 <- 本地仓库
$ git reset HEAD <file> # 本地仓库文件内容覆盖暂存区文件内容
# 本地仓库 <- 远程仓库
$ git clone <git_url> # 克隆远程仓库
$ git fetch upstream master # 拉取远程代码到本地但不应用在当前分支
$ git pull upstream master # 拉取远程代码到本地但应用在当前分支
$ git pull --rebase upstream master # 如果平时使用 rebase 合并代码则加上
# 工作区 <- 本地仓库
$ git reset <commit> # 本地仓库覆盖到工作区(保存回退文件内容修改)
$ git reset --mixed <commit> # 本地仓库覆盖到工作区(保存回退文件内容修改)
$ git reset --soft <commit> # 本地仓库覆盖到工作区(保留修改并加到暂存区)
$ git reset --hard <commit> # 本地仓库覆盖到工作区(不保留修改直接删除掉)
# 用户信息
$ git config --global user.name "your_name"
$ git config --global user.email "your_email"
# 文本编辑器
$ git config --global core.editor "nvim"
# 分页器
$ git config --global core.pager "more"
# 别名
$ git config --global alias.gs "git status"
# 纠错
$ git config --global help.autocorrect 1
# 不加 --global 参数的话,则为个人配置
$ git config --list
$ git config user.name
$ git config user.name "your_name"
# 如果在项目中设置,则保存在 .git/config文件里面
$ cat .git/config
[user]
name = "your_name"
......
# 3rd 的两个分支的 commit 修改相同内容
* 62a322d - (HEAD->master) Merge branch 'hotfix3' into master
|
| * 6fa8f4a - (hotfix3) 3rd commit in hotfix3
* | 548d681 - 3rd commit in master
|/
* 6ba4a08 - 2nd commit
* 22afcc1 - 1st commit
# 3rd 的两个分支的 commit 修改相同内容
* 697167e - (HEAD -> master, hotfix) 3rd commit
* 6ba4a08 - 2nd commit (2 minutes ago)
* 22afcc1 - 1st commit (3 minutes ago)
# 调整最近五次的提交记录
$ git rebase -i HEAD~5
$ git rebase -i 5af4zd35 # 往前第六次的commit值
reword c2aeb6e 3rd commit
squash 25a3122 4th commit
pick 5d36f1d 5th commit
fixup bd5d32f 6th commit
drop 581e96d 7th commit
# 查看提交历史记录
$ git log
* ce813eb - (HEAD -> master) 5th commit
* aa2f043 - 3rd commit -> modified
* 6c5418f - 2nd commit
* c8f7dea - 1st commit
编号 | 选项列表 | 对应含义解释 |
---|---|---|
1 | p /pick |
使用这个 commit 记录 |
2 | r /reword |
使用这个 commit 记录;并且修改提交信息 |
3 | e /edit |
使用这个 commit 记录;rebase 时会暂停允许你修改这个 commit |
4 | s /squash |
使用这个 commit 记录;会将当前 commit 与上一个 commit 合并 |
5 | f /fixup |
与 squash 选项相同;但不会保存当前 commit 的提交信息 |
6 | x /exec |
执行其他 shell 命令 |
7 | d /drop |
移除这个 commit 记录 |
# 不使用分页器
$ git --no-pager log --oneline -1
d5e96d9 (HEAD -> master) say file
# 改变提交信息并加入暂存区
$ echo "hello" > say.txt
$ git add -u
# 改变当前最新一次提交记录
$ git commit --amend
# 改变且息不改变提交信
$ git commit --amend --no-edit
# 改变当前最新一次提交记录并修改信息
$ git commit --amend -m "some_info"
# 不使用分页器
$ git --no-pager log --oneline -1
9e1e0eb (HEAD -> master) say file
# 回滚操作(可多次执行回滚操作)
# 彻底上次提交记录;也可是 PR 的提交记录
# 默认会生成一个类型为 reverts 的新 commit 对象
$ git revert 3zj5sldl
# 摘樱桃
$ git cherry-pick -x z562e23d
# 回退
$ git reset --hard <commit>
# 推送
$ git push origin master -f
# 分支
$ git branch -D <branch_name>
# 查看日志记录
$ git --no-pager log --oneline -1
4bc8703 (HEAD -> master) hhhh
# 回退到上次提交
$ git reset --hard HEAD~1
# 查看引用日志记录
$ git reflog
6a89f1b (HEAD -> master) HEAD@{0}: reset: moving to HEAD~1
4bc8703 HEAD@{1}: commit (amend): hhhh
# 找回内容
$ git cherry-pick 4bc8703
-
开源项目中使用了公司邮箱进行提交了
-
提交文件中包含隐私性的密码相关信息
-
提交时将大文件提交到了仓库代码中了
# 创建分支
$ git branch -b testing
# 修改邮箱地址
$ git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" == "[email protected]" ]; then
GIT_AUTHOR_NAME="escape";
GIT_AUTHOR_EMAIL="[email protected]";
git commit-tree "$@"
else
git commit-tree "$@"
fi' HEAD
➜ ll .git/hooks
total 112
-rwxr-xr-x applypatch-msg.sample
-rwxr-xr-x commit-msg.sample
-rwxr-xr-x fsmonitor-watchman.sample
-rwxr-xr-x post-update.sample
-rwxr-xr-x pre-applypatch.sample
-rwxr-xr-x pre-commit.sample
-rwxr-xr-x pre-merge-commit.sample
-rwxr-xr-x pre-push.sample # 不会推送包含WIP的commit提交
-rwxr-xr-x pre-rebase.sample
-rwxr-xr-x pre-receive.sample
-rwxr-xr-x prepare-commit-msg.sample
-rwxr-xr-x update.sample
# 安装方式
$ pip install pre-commit
# 指定 hook 类型(即在哪里检查)
$ pre-commit install -f --hook-type pre-push
# 配置需要执行的检查
$ cat .pre-commit-config.yaml
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.9.2
hooks:
- id: trailing-whitespace
- id: flake8
# 执行 push 操作时检查
$ git push origin master
# 克隆不包含之前历史
$ git clone http://xxx.xx.xxx/xxx --depth=1
# 克隆特定版本代码
$ git init xxx-15-0-1
$ git remote add origin http://xxx.xx.xxx/xxx
$ git -c protocol.version=2 fetch origin 15.0.1 --depth=1
$ git checkout FETCH_HEAD
# 克隆不包含 LFS 数据
$ GIT_LFS_SKIP_SMUDGE=1 git clone http://xxx.xx.xxx/xxx
➜ git stash -h
usage: git stash list [<options>]
or: git stash show [<options>] [<stash>]
or: git stash drop [-q|--quiet] [<stash>]
or: git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
or: git stash branch <branchname> [<stash>]
or: git stash clear
or: git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [-m|--message <message>]
[--pathspec-from-file=<file> [--pathspec-file-nul]]
[--] [<pathspec>...]]
or: git stash save [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
[-u|--include-untracked] [-a|--all] [<message>]
# 存储当前的修改但不用提交 commit
$ git stash
# 保存当前状态包括 untracked 的文件
$ git stash -u
# 展示所有 stashes 信息
$ git stash list
# 回到某个 stash 状态
$ git stash apply <stash@{n}>
# 删除储藏区
$ git stash drop <stash@{n}>
# 回到最后一个 stash 的状态并删除这个 stash 信息
$ git stash pop
# 删除所有的 stash 信息
$ git stash clear
# 从 stash 中拿出某个文件的修改
$ git checkout <stash@{n}> -- <file-path>
# 将工作区和暂存区覆盖最近一次提交
$ git commit --amend
$ git commit --amend -m "some_info"
# 回退到指定版本并记录修改内容(--mixed)
# 本地仓库覆盖到工作区(保存回退文件内容修改)
$ git reset a87f328
$ git reset HEAD~
$ git reset HEAD~2
$ git reset <tag>~2
$ git reset --mixed <commit/reference>
# 本地仓库覆盖到工作区(不保留修改直接删除掉)
$ git reset --soft <commit/reference>
# 本地仓库覆盖到工作区(保留修改并加到暂存区)
$ git reset --hard <commit/reference>
-
https://github.com/pre-commit/pre-commit
-
https://github.com/pre-commit/pre-commit-hooks
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论