git常用命令与commit提交规范
一、commit提交:安装与使用
1. 全局安装commitizen & cz-conventional-changelog
commitizen是一个撰写合格commit message的工具,用于代替git commit 指令,而cz-conventional-changelog适配器提供conventional-changelog标准(约定式提交标准)。
安装完毕后,可直接使用git cz来取代git commit。
npm install -g commitizen cz-conventional-changelog
echo '{ "path": "cz-conventional-changelog" }' > ~/.czrc
2. 项目内安装commitlint & husky
commitlint负责用于对commit message进行格式校验,husky负责提供更易用的git hook
#安装
npm install --save-dev @commitlint/config-conventional @commitlint/cli
#添加规范
echo "module.exports = {extends: ['@commitlint/config-conventional']};" > commitlint.config.js
@commitlint/cli: commitlint 的 CLI 工具
@commitlint/config-conventional: commitlint 的 conventional 规范配置方案,这是一个从 config-angular 衍生出的一个分支
husky: 一款 git hook 工具,可以 hook git 的命令
3. 在项目的package.json中添加commit钩子
{
"husky": {
"hooks": {
"commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
}
}
}
commit-msg 代表对 commit message 进行 hook,hook 的时候执行后面的命令 commitlint -E HUSKY_GIT_PARAMS 进行检查。
4.使用
# 执行git cz
git cz
# 进入interactive模式,根据提示依次填写
1.Select the type of change that you're committing 选择改动类型 (<type>)
2.What is the scope of this change (e.g. component or file name)? 填写改动范围 (<scope>)
3.Write a short, imperative tense description of the change: 写一个精简的描述 (<subject>)
4.Provide a longer description of the change: (press enter to skip) 对于改动写一段长描述 (<body>)
5.Are there any breaking changes? (y/n) 是破坏性修改吗?默认n (<footer>)
6.Does this change affect any openreve issues? (y/n) 改动修复了哪个问题?默认n (<footer>)
5.生成的commit message格式如下
填写完毕后,husky会调用commitlint对message进行格式校验,默认规定type及subject为必填项。
任何git commit指令的option都能用在 git cz指令上, 例如git cz -a
6.补充:commit规范
[
//主要tupe
feat: 新增feature,新功能
fix: 修复bug,错误修复
//特殊type
docs: 仅仅修改了文档,如readme.md
style: 仅仅是对格式进行修改,如逗号、缩进、空格等。不改变代码逻辑。
build: 构建工具的或者外部依赖的改动,例如webpack,npm
refactor: 生产代码重构,没有新增功能或修复bug
revert: 版本回滚
//暂不使用type
test: 测试用例,包括单元测试、集成测试。
perf: 优化相关,如提升性能、用户体验等。
ci:与CI(持续集成服务)有关的改动
chore: 不修改src或者test的其余修改,例如改变构建流程、增加依赖库、辅助工具的变动
];
二、git常用命令
git status
git stash
git stash pop
git add .
git commit
git commit -m "hello world" //提交
git checkout -b feature/common //切换并创建分支
git log 查看日志
git diff 文件url 查看文件修改前后差异
三、Git创建分支提交到远程分支步骤
#1. 创建一个自己的分支并切换过去
git checkout -b feature/skeleton
#2. 把本地分支push到远程
git push origin feature/skeleton
#3. 查看本地修改
git status
#4. 添加本地修改
git add .
#5.commit修改
git commit -m 'XXXX'
以后切换为
git cz
#6 push代码
git push origin feature/skeleton
#7. 查看分支
git branch
#8. 切换到主分支
git checkout master
#9. 更新
git pull