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

GitHub Actions无法强制推送到main分支,权限问题求助

GitHub Actions权限错误与分支同步问题解决

错误信息

! [remote rejected]     main -> main (refusing to allow a GitHub App to create or update workflow `.github/workflows/docker.yml` without `workflows` permission)

解决方案

1. 授予Workflows权限

解决权限不足问题,按以下步骤操作:

  • 打开目标GitHub仓库,进入Settings页面
  • 左侧导航选择Actions -> General
  • 找到Workflow permissions区域,选择Read and write permissions
  • 若需要,勾选Allow GitHub Actions to create and approve pull requests
  • 点击Save保存设置

设置完成后,GITHUB_TOKEN将拥有修改workflow文件的权限,推送时不会再触发权限错误。

2. 替代强制推送,避免手动冲突

你当前使用--force推送是因为变基后分支历史不一致,推荐两种更安全的方案:

方案一:使用现成的同步Action

直接用成熟的第三方Action处理上游同步,无需手动编写Git逻辑:

name: Update Fork
on:
  workflow_dispatch:
  schedule:
    - cron: '0 10 * * *'

jobs:
  sync-upstream:
    runs-on: ubuntu-latest
    steps:
      - name: Sync with upstream repo
        uses: aormsby/Fork-Sync-With-Upstream-action@v3.4
        with:
          upstream_repository: upstream_user_name/upstream_repo
          upstream_branch: main
          target_branch: main
          github_token: ${{ secrets.GITHUB_TOKEN }}

该Action会自动处理合并逻辑,有冲突时会终止运行并提醒,避免强制覆盖代码。

方案二:优化现有脚本

如果要保留自己的脚本,先修正变量错误(forked_commits未定义,应改为forked_commit),再改用安全的推送方式:

# 替换原有Check for Upstream Changes步骤的run内容
run: |
  git remote add upstream https://github.com/upstream_user_name/upstream_repo
  git fetch upstream

  # 检查分支是否已同步
  if git diff main upstream/main --quiet; then
    echo "No changes to sync from upstream."
  else
    # 使用合并而非变基,保留提交历史
    git merge upstream/main --no-edit
    # 权限解决后正常推送即可,无需--force
    git push origin main
    echo "Fork synced successfully."
  fi

如果出现冲突,脚本会失败,此时需要手动解决冲突后再运行,或添加冲突自动处理逻辑(如-X ours合并策略,但需谨慎使用)。

3. 关于你添加的repo-token

你在actions/checkout中添加repo-token: ${{ secrets.GITHUB_TOKEN }}的操作本身没问题,但必须确保该Token拥有读写权限(即前面设置的Workflow权限),否则权限错误仍会存在。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 11:50:24