git小工具---自动向其他分支merge代码

背景

项目分支比较多的时候,在临时分支开发完代码,要一个个的合并代码,过程很繁琐,希望把这个过程自动化

工具的作用是:

在临时分支开发完代码,执行git commit之后,会自动向指定的分支,进行merge操作,并push到远程仓库,一切都完成之后,会回到临时分支。

能够省去 git checkout,git pull,git merge,git push 再 git checkout .... 这些操作。

细节:

可选:只有git commit 内容带AM(可以自定义)字符的时候才会触发自动化流程,否则,不会触发,和普通的commit一样。

自定义:可以指定自动合并分支的列表,会依次去这些分支执行一系列操作

防呆防错: 可以指定排除的分支列表,在这些分支下,永远不会触发这个自动化流程

应用:

适用于项目分支比较多的情况,我用在法律法规项目上(这个项目有两个测试分支,每次合并代码,重复步骤多。)

实现

新建git钩子脚本: 在项目的.git/hooks文件夹中新建post-commit文件(shell脚本,没有后缀名),内容如下:

shell 复制代码
#!/bin/sh
  
# 日志记录函数

Log() {

  echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1"

}
# 获取项目根目录路径

PROJECT_ROOT=$(git rev-parse --show-toplevel)

Log "自动化流程开始..."

# 获取当前分支名称

CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)

# 定义需要排除的分支列表

EXCLUDE_BRANCHES=("release" "dev" "dev-test")

# 获取最新的提交信息
LAST_COMMIT_MESSAGE=$(git log -1 --pretty=%B)

# 检查提交信息中是否有 AM 标记

if [[ "$LAST_COMMIT_MESSAGE" =~ AM ]]; then
  RUN_DEPLOY=true
else
  RUN_DEPLOY=false

fi

# 检查当前分支是否在排除的分支列表中,并且是否包含 [deploy] 标记

if [[ ! " ${EXCLUDE_BRANCHES[@]} " =~ " ${CURRENT_BRANCH} " ]] && [ "$RUN_DEPLOY" = true ]; then

  Log "检测到提交到临时分支 $CURRENT_BRANCH,包含AM,开始自动化流程..."

  powershell -File "$PROJECT_ROOT/AM.ps1" -TempBranch "$CURRENT_BRANCH"

elif [ "$RUN_DEPLOY" = false ]; then

  Log "未检测到标记,跳过自动化流程。"

else

  Log "当前分支是 ${CURRENT_BRANCH},跳过自动化流程。"

fi

创建自动化脚本: windows环境下,在项目根目录 新建powershell自动化脚本 - AM.ps1

powershell 复制代码
# 定义参数

param (
    [string]$TempBranch,
    [string]$DevBranch = "dev",
    [string]$DevTestBranch = "dev-test",
    [string]$RemoteRepo = "origin"
)

function Log {
    param (
        [string]$message
    )
    Write-Output "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] $message"
}

# 检查是否提供了临时分支名称

if (-not $TempBranch) {    

  Log "error: temp banch name is not found"

    exit 1

}

# 检查是否在正确的分支上

$CURRENT_BRANCH = git rev-parse --abbrev-ref HEAD

if ($CURRENT_BRANCH -ne $TempBranch) {

    Log "error: current branch is not $TempBranch"

    exit 1

}

# 提交更改

git add .

if (-not (git diff-index --quiet HEAD --)) {

    git commit -m "feat: auto push from $TempBranch change"

}

else {

    Log "no change to push"

}
# 获取最新的远程dev和devTest分支

git checkout $DevBranch

git pull $RemoteRepo $DevBranch

git checkout $DevTestBranch

git pull $RemoteRepo $DevTestBranch

  
# 合并临时分支到dev和devTest分支

git checkout $DevBranch

git merge $TempBranch --no-ff -m "feat: auto merge $TempBranch to $DevBranch"

git checkout $DevTestBranch

git merge $TempBranch --no-ff -m "feat: auto merge $TempBranch to $DevTestBranch"

# 推送更新到远程仓库

Log "push $DevBranch ..."

git push $RemoteRepo $DevBranch

Log "push $DevTestBranch ..."

git push $RemoteRepo $DevTestBranch

git checkout $TempBranch

Log "over"
相关推荐
旅者时光2 小时前
Git使用基础
git
Clownorange3 小时前
git安装和配置
git
网安2311 013 小时前
OWASP ZAP 安全工具深度剖析:从环境搭建到架构复原的结对编程实践
git
ShineWinsu6 小时前
对于Linux:git版本控制器和cgdb调试器的解析
linux·c语言·git·gitee·github·调试·cgdb
php_kevlin7 小时前
git提交限制规范
大数据·git·elasticsearch
安大小万7 小时前
Git 常用命令终极指南:从入门到进阶
git
摇滚侠8 小时前
GIT 代码冲突 git pull 和 git pull rebase 的区别,保持提交记录的线性整齐
git
vistaup21 小时前
windows git 更新当前目录下所有的文件(非递归)
windows·git
王码码203521 小时前
Flutter for OpenHarmony:Flutter 三方库 algoliasearch 毫秒级云端搜索体验(云原生搜索引擎)
android·前端·git·flutter·搜索引擎·云原生·harmonyos
Irene19911 天前
Git 命令汇总表(基于一次完整的 Git 实战经验整理,涵盖从安装配置到日常开发、问题排查的所有常用命令)
git·常用命令