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

GitHub Actions Workflow报错及跨分支文件复制问题修复求助

解决GitHub Actions分支引用错误与文件复制问题

问题分析

你遇到的两个核心问题:

  • fatal: invalid reference: better-page:在gh-pages分支的工作目录中未拉取better-page分支的引用,导致无法检出该分支的文件。
  • Copy files步骤空提交:原find命令未正确排除不需要的目录,且未提前创建目标目录,导致实际未复制有效文件,git检测不到变更。

修改后的完整Workflow代码

name: Copy files to another branch

on:
    push:
        branches:
            - better-page
    workflow_dispatch:

permissions:
    contents: write
    pages: write
    id-token: write

concurrency:
    group: "pages"
    cancel-in-progress: false

jobs:
    copy:
        runs-on: ubuntu-latest
        steps:
            - name: Checkout better-page
              uses: actions/checkout@v3
              with:
                  ref: better-page
                  fetch-depth: 0 # 拉取全部分支历史,避免引用缺失

            - name: Copy files to simplified directory (better-page branch)
              run: |
                  git config --global user.name "GitHub Actions"
                  git config --global user.email "action@github.com"
                  # 确保目标目录存在
                  mkdir -p simplified
                  # 用rsync精准复制并排除不需要的目录
                  rsync -r --exclude=.git --exclude=.github --exclude=about-me --exclude=node_modules --exclude=simplified ./ simplified/
                  # 检查变更并提交
                  if git diff --quiet; then
                      echo "No changes to commit in better-page"
                  else
                      git add simplified/
                      git commit -m "Update simplified directory with latest better-page files"
                      git push origin better-page
                  fi

            - name: Checkout gh-pages branch
              uses: actions/checkout@v3
              with:
                  ref: gh-pages
                  path: gh-pages
                  fetch-depth: 0 # 拉取全部分支,确保能访问better-page

            - name: Sync simplified directory to gh-pages
              run: |
                  git config --global user.name "GitHub Actions"
                  git config --global user.email "action@github.com"
                  # 拉取better-page分支最新内容
                  git fetch origin better-page
                  # 从远程better-page分支检出simplified目录
                  git checkout origin/better-page -- simplified
                  # 检查变更并提交
                  if git diff --quiet; then
                      echo "No changes to sync to gh-pages"
                  else
                      git add simplified/
                      git commit -m "Sync simplified directory from better-page"
                      git push origin gh-pages
                  fi
              working-directory: gh-pages

关键修改点说明

1. 修复分支引用错误

  • 在两次actions/checkout中添加fetch-depth: 0:默认checkout只拉取当前分支的浅克隆,添加该参数后会拉取仓库所有分支的完整历史,确保后续操作能找到better-page分支的引用。
  • 改用origin/better-page检出文件:在gh-pages工作目录中,直接引用远程分支而非本地分支,避免本地未跟踪该分支导致的引用无效问题。

2. 修复空提交问题

  • 恢复rsync命令:相比find,rsync能更精准地排除不需要的目录(如.git、.github等),避免复制git忽略的文件,确保有效文件被复制到simplified目录。
  • 提前创建simplified目录:添加mkdir -p simplified确保目标目录存在,避免复制命令因目录不存在而执行失败。
  • 优化变更检测逻辑:用git diff --quiet直接检查文件变化,只提交simplified/目录的变更,逻辑更清晰,避免误提交其他修改。

3. 其他优化

  • 重命名步骤名称,提升流程可读性。
  • 添加无变更时的提示信息,方便调试。
  • 简化提交信息,明确操作目的。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 04:12:02