You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否让GitHub自动将源分支与目标分支添加至PR标题?

自动给GitHub PR标题添加分支名称的实现方法

不用手动添加分支信息,靠GitHub Actions就能实现完全自动化,具体操作如下:

  • 在仓库的.github/workflows/目录下新建一个配置文件,比如命名为pr-title-auto-add-branch.yml
  • 设置触发规则:当PR被创建、同步或者标题被编辑时,自动运行这个工作流
  • 借助actions/github-script编写脚本,获取PR的源分支与目标分支,自动给标题加上指定格式的前缀

直接用下面的配置代码即可:

name: 自动添加分支信息到PR标题

on:
  pull_request:
    types: [opened, synchronize, edited]

jobs:
  update-pr-title:
    runs-on: ubuntu-latest
    steps:
      - name: 更新PR标题
        uses: actions/github-script@v6
        with:
          script: |
            const pr = context.payload.pull_request;
            const sourceBranch = pr.head.ref;
            const targetBranch = pr.base.ref;
            const prefix = `[${targetBranch} < ${sourceBranch}]`;
            
            // 避免重复添加前缀
            if (!pr.title.startsWith(prefix)) {
              const newTitle = `${prefix} ${pr.title}`;
              await github.rest.pulls.update({
                owner: context.repo.owner,
                repo: context.repo.repo,
                pull_number: pr.number,
                title: newTitle
              });
            }
  • 将这个文件提交到仓库后,后续所有符合触发条件的PR都会自动带上[目标分支 < 源分支]格式的标题前缀
  • 如果需要自定义前缀格式,比如调整分支顺序、替换分隔符号,直接修改代码里的prefix变量内容就行

另外要说明的是,PR模板无法自动填充分支信息,用GitHub Actions的方式是最省心的,全程不需要手动干预。

内容的提问来源于stack exchange,提问作者Sven Martoen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 18:03:11