git 常用命令

  1. git add . 将所有改动放进暂存区
  2. git commit -m "描述" 提交并附带概要信息
  3. git pull 从远程仓库拉去代码
  4. git push 推送代码到远程仓库(master分支)
  5. git log 查看日志
  6. git log -p 查看详细历史
  7. git log --stat 查看简要统计
  8. git status 查看工作区状态
  9. git branch 名称 创建分支
  10. git checkout 名称 切换分支
  11. git checkout -b 名称 创建并切换到新分支
  12. git branch -d 名称 删除该分支(不能删除当前所在的分支,不能删除没有合并到master上的分支)
  13. git branch -D 名称 删除该分支(可以删除没有合并到master上的分支)
  14. git commit --amend 对最新的一条commit进行修正
  15. git reset --hard HEAD^ 丢弃最新提交(未提交的内容会被擦掉)
  16. git reset --soft HEAD^ 丢弃最新提交(未提交的内容不会被擦掉)
  17. git revert HEAD^ 回到某个commit
  18. git rebase 目标基础点 重新设置基础点
  19. git merge 名称 将分支合并到head指向的分支
  20. git push origin localbranch 将代码推送到远程仓库的指定分支
  21. git push -d origin branchName 删除远程分支
  22. git stash 暂存代码
  23. git stash pop 弹出暂存代码

git Hooks

what is

Git Hooks 就是在Git执行某个操作(如commit、push等)后触发运行的脚本

why use

  1. 团队开发,编码风格不同,导致代码难看,难以维护
  2. 良好的规范性,有助于代码审查和后期维护
  3. 提高代码可读性

    how use

  • husky:对git执行的一些命令,通过对应的hooks钩子触发,执行自定义的脚本程序 ```bash

    安装husky7 version

    npm install husky —save-dev

初始化.husky目录以及pre-commit shell脚本文件 (见图一)

npx husky-init

若从高版切换为低版本(如7->4)

rm -rf .husky && git config —unset core.hooksPath

  1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/2828912/1643265664562-b6d15240-3b75-49a9-87e3-715f041f66e6.png#clientId=u047f0906-4d62-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=322&id=FstZl&margin=%5Bobject%20Object%5D&name=image.png&originHeight=432&originWidth=412&originalType=binary&ratio=1&rotation=0&showTitle=true&size=16322&status=done&style=none&taskId=u38aae88e-51f5-4df3-8ecd-83690450969&title=%E5%9B%BE%E4%B8%80%EF%BC%88husky-init%EF%BC%89&width=307 "图一(husky-init)")![image.png](https://cdn.nlark.com/yuque/0/2022/png/2828912/1643265783225-465c7b8f-ffe7-4d25-a211-959504bdc22f.png#clientId=u047f0906-4d62-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=142&id=lEieF&margin=%5Bobject%20Object%5D&name=image.png&originHeight=129&originWidth=330&originalType=binary&ratio=1&rotation=0&showTitle=true&size=7031&status=done&style=none&taskId=u8c9b911e-6e5c-4414-bbb5-e4130ee652a&title=%E5%9B%BE%E4%BA%8C%EF%BC%88pre-commit%E4%B8%ADshell%E8%84%9A%E6%9C%AC%EF%BC%89&width=362 "图二(pre-commit中shell脚本)")![image.png](https://cdn.nlark.com/yuque/0/2022/png/2828912/1643265806992-f4f074e8-e6cd-47b4-b706-d6a364241cee.png#clientId=u047f0906-4d62-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=122&id=zivhF&margin=%5Bobject%20Object%5D&name=image.png&originHeight=122&originWidth=588&originalType=binary&ratio=1&rotation=0&showTitle=true&size=20935&status=done&style=none&taskId=u0b8f7032-3453-4104-a3f8-58ab5697b5a&title=%E5%9B%BE%E4%B8%89%EF%BC%88%E6%89%A7%E8%A1%8Cgit%20commit%E5%90%8E%E6%89%A7%E8%A1%8C%E8%84%9A%E6%9C%AC%E7%9A%84%E8%BE%93%E5%87%BA%EF%BC%89&width=588 "图三(执行git commit后执行脚本的输出)")
  2. <a name="ugJ3b"></a>
  3. # 编码规范及代码提交规范相关
  4. <a name="ruS8d"></a>
  5. ## eslint:
  6. - 插件化JavaScript代码检测工具 Js编码规范,检测并提示错误或警告信息
  7. ```bash
  8. npm install eslint --save-dev
  9. # 自动生成.eslintrc.js文件
  10. npx eslint --init

prettier:

  • 代码格式化工具 ,代码风格管理,更好的代码风格效果 ```bash npm install prettier —save-dev —save-exact

若有eslint需要安装相关库(在.eslintrc 中,extend中添加 “prettier” 解决 eslint 和 prettier 的冲突)

npm install eslint-config-prettier eslint-plugin-prettier —save-dev

需在根目录下创建.prettierrc文件

若有需要根目录下.prettierignore来忽略prettier检测

  1. <a name="ubWoN"></a>
  2. ## lint-staged:
  3. - 只检测暂存区的文件,并执行脚本
  4. ```bash
  5. npm install lint-staged --save-dev
  1. // package.json
  2. {
  3. "scripts": {
  4. "prettier": "prettier --write '**/*.{js,jsx,tsx,ts,less,md,json}'",
  5. },
  6. "lint-staged": {
  7. "*.{jsx,less,md,json}": [
  8. "prettier --write"
  9. ],
  10. "*.ts?(x)": [
  11. "prettier --parser=typescript --write"
  12. ]
  13. }
  14. }

commitlint:

  • 代码提交检测,检测git commit 内容是否符合定义的规范 ```bash

    1. 使用三方库

npm install —save-dev @commitlint/config-conventional @commitlint/cli

在.husky目录中新建commit-msg钩子文件并添加以下脚本,在进行commit时候进行检测

npx —no-install commitlint —edit $1

2. 自定义commit-msg命令检测

新增npm scripts

“commit-msg”:”node ./scripts/verifyCommit.js”

在.husky目录中新建commit-msg钩子文件并添加以下脚本,在进行commit时候进行检测

npm run commit-msg — $1

  1. <a name="vVTbk"></a>
  2. ## verifyCommit:
  3. - 对commit-msg进行校验(注:chalk版本为: 4.1.1,新版本只支持esModule,而不支持cjs)
  4. ```bash
  5. const chalk = require("chalk");
  6. const msgPath = process.argv.splice(2)[0];
  7. const msg = require("fs").readFileSync(msgPath, "utf-8").trim();
  8. const commitRE =
  9. /^(revert: )?(feat|fix|docs|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release|dep)(\(.+\))?: .{1,50}/;
  10. if (!commitRE.test(msg)) {
  11. console.error(
  12. ` ${chalk.bgRed.white(" ERROR ")} ${chalk.red(
  13. `invalid commit message format.`
  14. )}\n\n` +
  15. chalk.red(
  16. ` Proper commit message format is required for automated changelog generation. Examples:\n\n`
  17. ) +
  18. ` ${chalk.green(`type(scope): describe`)}\n\n` +
  19. chalk.red(` See .github/commit-convention.md for more details.\n`)
  20. );
  21. process.exit(1);
  22. }

commitizen:

  • 代码提交内容标准化 ,提示定义输入标准的git commit 内容 ```bash npm install commitizen -g

git添加到暂存区后使用giz cz命令进行填写(注:不支持bash)

git cz ```