#场景
公司的工作流是:test 是长期分支,每个版本持续使用,推送到 test 自动触发测试环境部署;feature_xx 分支一般只留在本地,不推远程
feature 单独推送远程、被 master 合并,一般只在修线上问题时发生,随后 test 会再合并一次 master 同步修复
日常在不同的功能分支上开发,每次想看效果都要手动走一遍:
bashgit checkout test git pull git merge feature-xxx git push git checkout feature-xxx
来回切分支很繁琐,merge 还会产生一条合并提交
于是封装了两个 alias:常规验证用
git test,在当前分支一条命令完成检查工作区 → 拉最新 origin/test → rebase → 推送,全程不切分支;线上修复的收尾链路用 git hotfix#git test
bashgit config --global alias.test '!f() { \ if ! git diff --quiet || ! git diff --cached --quiet; then \ echo "❌ 工作区有未提交修改,请先 commit 或 stash"; \ exit 1; \ fi; \ echo "⬇️ Fetch origin/test..."; \ git fetch origin test || exit 1; \ echo "🔄 Rebase onto origin/test..."; \ git rebase origin/test || { \ echo "❌ Rebase 冲突。解决后执行 git rebase --continue,或 git rebase --abort"; \ exit 1; \ }; \ echo "🚀 Push HEAD -> origin/test..."; \ git push origin HEAD:test; \ }; f'
配置好后,任意分支上提交完代码,执行:
bashgit test
#git hotfix
修线上问题的链路是 master 合并 hotfix 分支、test 再合并一次 master,收尾同样四五步,也封装掉:
bashgit config --global alias.hotfix '!f() { \ branch=$(git branch --show-current); \ if [ "$branch" = "master" ] || [ "$branch" = "test" ]; then \ echo "❌ 请切到 hotfix 分支上执行"; \ exit 1; \ fi; \ if ! git diff --quiet || ! git diff --cached --quiet; then \ echo "❌ 工作区有未提交修改,请先 commit 或 stash"; \ exit 1; \ fi; \ echo "⬇️ Fetch origin..."; \ git fetch origin || exit 1; \ echo "📦 master 合并 $branch..."; \ git checkout master || exit 1; \ git merge --ff-only origin/master || { echo "❌ 本地 master 与远程分叉,请先手动处理"; exit 1; }; \ git merge "$branch" || { echo "❌ 冲突。解决后 git add . && git commit,重新执行 git hotfix"; exit 1; }; \ git push origin master || exit 1; \ echo "📦 test 合并 master..."; \ git checkout -B test origin/test || exit 1; \ git merge master || { echo "❌ 冲突。解决后 git add . && git commit && git push origin test"; exit 1; }; \ git push origin test || exit 1; \ echo "↩️ 回到 $branch"; \ git checkout "$branch"; \ }; f'
使用:从 master 拉 hotfix 分支,修复提交后执行:
bashgit checkout -b hotfix_xxx origin/master # 修复并 commit 后 git hotfix
test 推送后自动触发测试环境部署,正好验证修复
几个细节:
- 先记住当前分支,收尾后自动切回来;在 master / test 上执行会直接拒绝
- master 用
--ff-only对齐远程,本地 master 与远程分叉时直接停下,不做自动合并 - 本地 test 用
-B重置到 origin/test 再合并 master,始终以远程为准(流程里没人直接往本地 test 提交,重置安全) - master 合并冲突时,解决 commit 后重新执行
git hotfix会接着走完剩余步骤;test 合并冲突按提示手动 push 即可
#注意
修线上问题的分支慎跑
git test:rebase 到 origin/test 后,分支历史里会混入 test 上还没发版的功能,master 直接合并它,未发版的代码就跟着上线了hotfix 从 master 拉分支、不跑
git test,收尾用 git hotfix 完成 master 合并与 test 同步;实在要先上测试环境验证,合 master 时改用 squash merge#alias 管理
bash# 查看已配置的所有 alias git config --get-regexp '^alias\.' # 删除这个 alias git config --global --unset alias.test # 或者直接编辑 ~/.gitconfig 的 [alias] 段