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

如何在GitHub Action中将文件推送到另一仓库?解决origin已存在报错

跨仓库同步数据的GitHub Action报错解决

问题描述

我有一个存储代码的私有仓库,每天通过GitHub Action运行代码。近期需要创建新的公开仓库展示数据(例如Kow.csv),希望每天通过GitHub Action将该文件从私有仓库复制到公开仓库。目前使用的Workflow代码运行失败,报错信息为:error: remote origin already exists.

当前使用的Workflow代码:

name: test
'on':
  schedule:
    - cron: '0 22 * * *'
  workflow_dispatch:
jobs:
  craw_process_bot:
    runs-on: ubuntu-latest
    steps:
      - name: check_code
        uses: actions/checkout@master
      - name: set up Python 3.8
        uses: actions/setup-Python@v2
        with:
          python-version: '3.8'
      - name: install requirements
        run: |
          python -m pip install --upgrade pip
          pip install pandas
          ...
          pip install pyautogui
      - name: process data
        run: python ./code/global_code/all_process.py
      - name: commit and push if it changed
        run: |
          git config user.name "${GITHUB_ACTOR}"
          git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
          git add -A
          timestamp=$(date -u)
          git commit -m "Last updated on: ${timestamp}" || exit 0
          git remote add origin https://github.com/###/###.git # address of the public repo
          git push -u origin master

报错原因

Action检出私有仓库后,仓库默认的远程名称就是origin,再次执行git remote add origin会尝试添加同名远程,导致冲突报错。

解决方案

有两种可行的修改方式:

方式一:替换现有origin远程地址

把添加远程的命令改成替换现有地址,直接推送到公开仓库:

git remote set-url origin https://github.com/###/###.git # 替换为公开仓库地址
git push -u origin master

方式二:新增自定义名称的远程仓库

如果不想替换原有origin,可以给公开仓库起一个新的远程别名(比如public),再推送:

git remote add public https://github.com/###/###.git # 新增名为public的远程
git push -u public master

额外注意事项

  • 权限问题:默认的GITHUB_TOKEN仅对当前私有仓库有操作权限,跨仓库推送需要使用拥有公开仓库写入权限的Personal Access Token(PAT)。在私有仓库的Secrets中配置该PAT后,push命令格式改为:https://<你的PAT>@github.com/###/###.git
  • 文件同步:确保all_process.py生成的Kow.csv被git add -A包含,或明确指定添加该文件(比如git add Kow.csv),避免遗漏要同步的目标文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 09:18:30