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

如何用GitHub Actions将分支内容复制到另一分支指定文件夹及报错解决

问题描述

我有三个分支:

  • main(仅包含一个README.md文件)
  • my-branch(包含若干文件、文件夹及子文件夹)
  • gh-pages(用于GitHub Pages部署)

需求:当my-branch分支有推送操作时,将该分支的所有内容复制到gh-pages分支下名为myFolder的文件夹中。

最初尝试仅复制变更文件(比如用rsync),但难度较高,因此改为每次推送时全量复制;若有更优方案也请告知。

我编写了以下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@v2
              with:
                  ref: better-page

            - name: Copy files
              run: |
                  git config --global user.name "GitHub Actions"
                  git config --global user.email "action@github.com"
                  rsync -r --exclude=.git --exclude=.github --exclude=about-me --exclude=node_modules --exclude=simplified . simplified/
                  git add .
                  git diff-index --quiet HEAD ||  git commit -am "create simplified directory"  # commit to the repository (ignore if no modification)
                  git push origin better-page # push to remote branch
                  git checkout gh-pages
                  git fetch origin gh-pages
                  git pull origin gh-pages
                  git push origin gh-pages
                  git checkout better-page
                  git fetch origin better-page
                  git pull origin better-page
                  git push origin better-page
                  git checkout gh-pages
                  git checkout remotes/origin/better-page -- simplified
                  git add -A
                  git diff-index --quiet HEAD ||  git commit -am "deploy files"
                  git push origin gh-pages
                  git checkout better-page
                  rm -rf simplified
                  git add -A
                  git diff-index --quiet HEAD ||  git commit -am "remove simplified directory"
                  git push origin better-page

报错信息:

Everything up-to-date
error: pathspec 'gh-pages' did not match any file(s) known to git
Error: Process completed with exit code 1.

报错原因分析

  1. 本地未拉取gh-pages分支:使用actions/checkout@v2时指定了ref: better-page,默认只会拉取该分支代码,本地仓库不存在gh-pages分支,执行git checkout gh-pages自然报错。
  2. 脚本逻辑冗余混乱:来回切换分支、重复推送的操作完全没必要,既增加出错概率,也浪费资源。

修复后的Workflow实现

以下是简化且正确的实现,可实现当my-branch推送时,全量复制内容到gh-pages的myFolder目录:

name: Sync my-branch to gh-pages/myFolder
on:
  push:
    branches:
      - my-branch  # 替换为你的目标分支名
  workflow_dispatch:  # 支持手动触发

permissions:
  contents: write

jobs:
  sync-files:
    runs-on: ubuntu-latest
    steps:
      # 1. 检出仓库所有分支,确保获取到gh-pages
      - name: Checkout full repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0  # 拉取完整的仓库历史和所有分支
          
      # 2. 配置Git用户信息
      - name: Configure Git user
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "action@github.com"
          
      # 3. 切换到gh-pages分支并拉取最新代码
      - name: Switch to gh-pages and pull latest
        run: |
          git checkout gh-pages
          git pull origin gh-pages
          
      # 4. 清空myFolder(如果存在),确保全量覆盖
      - name: Clear existing myFolder
        run: |
          rm -rf myFolder || true
          mkdir -p myFolder
          
      # 5. 从my-branch分支复制所有需要的文件到myFolder
      # 可添加--exclude忽略不需要的文件/文件夹
      - name: Copy files from my-branch to myFolder
        run: |
          git checkout my-branch -- .  # 暂存my-branch的所有文件
          rsync -r --exclude=.git --exclude=.github --exclude=node_modules ./ myFolder/
          git reset HEAD .  # 取消暂存,避免影响gh-pages分支状态
          
      # 6. 提交并推送到gh-pages
      - name: Commit and push changes
        run: |
          git add myFolder/
          # 只有当有变更时才提交
          git diff-index --quiet HEAD || git commit -m "Sync my-branch to myFolder [Automated]"
          git push origin gh-pages

更优方案建议

如果需要更简洁可靠的实现,推荐使用git worktree管理多分支,避免频繁切换分支的麻烦:

name: Sync my-branch to gh-pages/myFolder
on:
  push:
    branches:
      - my-branch
  workflow_dispatch:

permissions:
  contents: write

jobs:
  sync-with-worktree:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
        with:
          fetch-depth: 0
          
      - name: Configure Git user
        run: |
          git config --global user.name "GitHub Actions"
          git config --global user.email "action@github.com"
          
      # 创建gh-pages分支的工作树目录
      - name: Setup gh-pages worktree
        run: |
          git worktree add gh-pages-dir gh-pages
          
      # 清空并复制文件到工作树的myFolder
      - name: Sync files to myFolder
        run: |
          rm -rf gh-pages-dir/myFolder || true
          mkdir -p gh-pages-dir/myFolder
          rsync -r --exclude=.git --exclude=.github --exclude=node_modules ./ gh-pages-dir/myFolder/
          
      # 提交并推送gh-pages分支
      - name: Commit and push
        run: |
          cd gh-pages-dir
          git add myFolder/
          git diff-index --quiet HEAD || git commit -m "Sync my-branch to myFolder [Automated]"
          git push origin gh-pages
          
      # 清理工作树
      - name: Cleanup worktree
        run: |
          git worktree remove gh-pages-dir

这种方式通过独立工作目录操作gh-pages分支,逻辑更清晰,也减少了分支切换带来的潜在问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 00:15:28