增强版 Git:支持高级工作流、Git 钩子、CI/CD 集成、单体仓库(monorepo)支持、Git LFS,以及 Git 内部机制深度解析。
Git Plus. 增强Git 具有高级工作流程,悬钩,以及 CI/CD 集成.是一项面向实际任务的技能,主要用于Features. 高级工作流程: Gitflow, 树干基, GitHub Flow.;Git Hooks : 预承诺,预推,承诺-msg hooks.。
实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;
涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
增强版 Git,支持高级工作流、钩子(hooks)及 CI/CD 集成。
| 任务 | 命令 |
|---|---|
| 状态 | git status -sb |
| 日志 | git log --oneline -10 --graph |
| 差异 | git diff --stat HEAD~5 |
| 暂存 | git stash push -m "description" |
| 变基 | git rebase -i HEAD~5 |
# 功能分支 git checkout -b feature/new-feature develop git commit -m "feat: add new feature" git checkout develop git merge --no-ff feature/new-feature # 发布分支 git checkout -b release/1.0.0 develop git commit -m "chore: bump version" git checkout main git merge --no-ff release/1.0.0 git tag -a v1.0.0 -m "Release 1.0.0" # 热修复分支 git checkout -b hotfix/urgent-fix main git commit -m "fix: urgent issue" git checkout main git merge --no-ff hotfix/urgent-fix
# 生命周期较短的功能分支 git checkout -b feature/short-lived git commit -m "feat: quick change" git push origin feature/short-lived # 创建 PR 并合并至 main # 直接提交(适用于小型变更) git checkout main git pull origin main git commit -m "feat: small change" git push origin main
# 创建分支 git checkout -b feature/my-feature # 修改代码 git add . git commit -m "feat: add feature" # 推送并创建 PR git push origin feature/my-feature # 通过 GitHub UI 创建 PR # 审查通过后,通过 GitHub 合并
#!/bin/bash # .git/hooks/pre-commit # 运行代码检查器(linter) npm run lint # 运行测试 npm test # 检查敏感信息 if grep -r "password|secret|api_key" --include="*.js" --include="*.ts" .; then echo "Error: Potential secrets found" exit 1 fi
#!/bin/bash
# .git/hooks/commit-msg
# 校验约定式提交(Conventional Commit)格式
commit_msg=$(cat "$1")
pattern="^(feat|fix|docs|style|refactor|test|chore)((.+))?: .{1,72}"
if ! echo "$commit_msg" | grep -qE "$pattern"; then
echo "Error: Invalid commit message format"
echo "Format: type(scope): description"
echo "Example: feat(auth): add login page"
exit 1
fi
#!/bin/bash
# .git/hooks/pre-push
# 推送前运行测试
npm test
# 检查是否为强制推送(force push)
for ref in "$@"; do
if echo "$ref" | grep -qE "refs/heads/(main|master|develop)$"; then
read -p "Force push to protected branch? (y/n) " -n 1 -r
echo
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
exit 1
fi
fi
done
# 将钩子脚本复制到 .git/hooks 目录 cp hooks/* .git/hooks/ # 或使用 Husky(Node.js) npx husky install npx husky add .git/hooks/pre-commit "npm run lint"
# .github/workflows/ci.yml
name: CI
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '20'
- run: npm ci
- run: npm test
- run: npm run lint
deploy:
needs: test
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run build
- run: npm run deploy
# .gitlab-ci.yml
stages:
- test
- build
- deploy
test:
stage: test
script:
- npm ci
- npm test
- npm run lint
build:
stage: build
script:
- npm ci
- npm run build
artifacts:
paths:
- dist/
deploy:
stage: deploy
script:
- npm run deploy
only:
- main
# 为功能分支添加工作树 git worktree add ../feature-branch feature/branch # 列出所有工作树 git worktree list # 删除工作树 git worktree remove ../feature-branch
# 启用稀疏检出 git sparse-checkout init --cone # 设置需检出的目录 git sparse-checkout set packages/frontend packages/shared # 禁用稀疏检出(恢复完整检出) git sparse-checkout disable
# 添加子模块 git submodule add https://github.com/user/repo.git libs/repo # 更新子模块(含递归初始化) git submodule update --init --recursive # 克隆时同步拉取子模块 git clone --recurse-submodules https://github.com/user/repo.git
# 查看对象类型与大小 git cat-file -tgit cat-file -s # 查看对象内容 git cat-file -p # 列出所有对象 git rev-list --objects --all
# 查看引用 git show-ref # 查看引用日志(reflog) git reflog # 查找包含某次提交的所有分支 git branch --contains
# 生成打包文件 git gc # 校验打包文件 git verify-pack -v .git/objects/pack/*.idx # 查看打包文件统计信息 git count-objects -v
# 对最近 5 次提交执行交互式变基 git rebase -i HEAD~5 # 可用操作: # p, pick = 保留该提交 # r, reword = 保留该提交,但编辑其提交信息 # e, edit = 保留该提交,进入修改模式(amend) # s, squash = 合并到前一次提交中 # f, fixup = 类似 squash,但丢弃本次提交的信息 # d, drop = 删除该提交
# 拣选单次提交 git cherry-pick# 拣选提交范围 git cherry-pick .. # 拣选但不自动提交 git cherry-pick --no-commit
# 启动二分查找 git bisect start git bisect bad git bisect good v1.0.0 # Git 自动检出中间提交 # 手动测试后,标记为 good 或 bad git bisect good # 或 git bisect bad # 重置二分查找状态 git bisect reset