git 的基本使用
安装 git
官网下载、安装(一直 next 就行了)
配置密钥 ssh
查看当前用户信息:
git config user.namegit config user.email设置提交代码时的用户信息:
git config --global user.name "raopan2021"git config --global user.email "raopan2021@outlook.com"创建
ssh
ssh-keygen -t rsa -C 'raopan2021@outlook.com'之后不断Enter即可
查看你生成的公钥
cat ~/.ssh/id_rsa.pub输入该命令回车后,复制看到的公钥,是类似于这样的一串字符
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDY01oB1yY4wbZj8T/
·······
kf82w4u+RZwyu4E20I6p7boP/EKjGXiDBw10/Qc= raopan2021@outlook.com2
3
git同时提交到 gitee、github
找到 .git 文件夹下面的 config文件修改,新增行:
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
url = https://github.com/raopan2021/blog.git
url = https://gitee.com/raopan2021/blog.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main2
3
4
5
6
7
8
9
10
11
12
13
14
也就是说,在原来的github仓库地址下面再添加一个url配置,指向gitee的地址。
当然,这里有一个前提条件,Gitee和GitHub的账号的公私钥为同一套。
此时再修改本地代码,进行提交,你会发现GitHub和Gitee上的代码同时被修改了。是不是很cool?
git commit 提交规范 & 规范校验
在多人协作项目中,如果代码风格统一、代码提交信息的说明准确,那么在后期协作以及Bug处理时会更加方便。
下面介绍怎么使用下面这个工具,在git push 代码之前检测 commit messages
Commit message格式
<type>: <subject>
注意冒号后面有空格。
type
用于说明 commit 的类别,只允许使用下面7个标识。
- feat:新功能(feature)
- fix:修补bug
- docs:文档(documentation)
- style: 格式(不影响代码运行的变动)
- refactor:重构(即不是新增功能,也不是修改bug的代码变动)
- test:增加测试
- chore:构建过程或辅助工具的变动
如果type为feat和fix,则该 commit 将肯定出现在 Change log 之中。
subject
subject是 commit 目的的简短描述,不超过50个字符,且结尾不加句号(.)。
使用工具校验commit是否符合规范
commitlint
全局安装
npm install -g @commitlint/cli @commitlint/config-conventional生成配置配件
这个文件在根目录下生成就可以了。
echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js在commitlint.config.js制定提交message规范
"module.exports = {extends: ['@commitlint/config-conventional']}"
module.exports = {
extends: ['@commitlint/config-conventional'],
rules: {
'type-enum': [2, 'always', [
"feat", "fix", "docs", "style", "refactor", "test", "chore", "revert"
]],
'subject-full-stop': [0, 'never'],
'subject-case': [0, 'never']
}
};2
3
4
5
6
7
8
9
10
11
12
上面我们就完成了commitlint的安装与提交规范的制定。检验commit message的最佳方式是结合git hook,所以需要配合Husky
husky
husky继承了Git下所有的钩子,在触发钩子的时候,husky可以阻止不合法的commit,push等等。注意使用husky之前,必须先将代码放到git 仓库中,否则本地没有.git文件,就没有地方去继承钩子了。
npm install husky --save-dev安装成功后需要在项目下的package.json中配置
"scripts": {
"commitmsg": "commitlint -e $GIT_PARAMS",
},
"config": {
"commitizen": {
"path": "cz-customizable"
}
},2
3
4
5
6
7
8
9
最后我们可以正常的git操作
可以在package.json下面添加如下的钩子。
"husky": {
"hooks": {
"pre-commit": "npm run lint"
}
},2
3
4
5
可参考 Vue 2.7 + Vite 脚手架 项目的配置