Github github.com自动同步Sync fork上游项目代码仓库 - 全文内容:
显示技术博客列表Github github.com自动同步Sync fork上游项目代码仓库
超短链接Github github.com自动同步Sync fork上游项目代码仓库
1、添加.github/workflows/sync-upstream.yml文件:
2、找到当前的主要分支是main还是master:
3、.github/workflows/sync-upstream.yml文件内容:
name: Sync Upstream on: schedule: - cron: '0 0 * * *' # 每天 00:00 UTC 运行 workflow_dispatch: # 允许手动触发 jobs: sync: runs-on: ubuntu-latest steps: - name: 检出你的 Fork 仓库 uses: actions/checkout@v4 with: persist-credentials: false - name: 配置 Git 用户信息 run: | git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" - name: 配置 GitHub 身份验证 run: | git config --global credential.helper 'store' echo "https://github-actions[bot]:${{ secrets.GITHUB_TOKEN }}@github.com" > ~/.git-credentials - name: 添加上游仓库,与保留本地.github/workflows/ 目录内容不更新 run: | git remote add upstream https://github.com/Guovin/iptv-api.git || true # 这里改为你的上游仓库 git fetch upstream git checkout master # 这里改为你的上游主要分支,一般可能是main或者master git checkout -b temp-merge # 创建一个临时分支 git merge -X theirs upstream/master --allow-unrelated-histories # 默认保留上游代码 这里改为你的上游主要分支,一般可能是main或者master git checkout master # 回到主分支这里改为你的上游主要分支,一般可能是main或者master rsync -a .github/workflows/ ../workflows-backup/ # 备份工作流目录 git reset --hard temp-merge # 硬重置到合并后的状态 rsync -a ../workflows-backup/ .github/workflows/ # 恢复工作流目录 rm -rf ../workflows-backup # 删除临时备份 git add .github/workflows/ git commit -m "同步上游代码,并保留本地 workflows" || true git branch -D temp-merge # 删除临时分支 - name: 推送到 Fork 仓库 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | git push origin master --force --no-verify # 这里改为你的上游主要分支,一般可能是main或者master
也可以直接下载模板文件:
_.github_workflows_sync-upstream-模板.rar (可能上漩更新新增了上游计划导致同步失败)
20250309模板修定版本2,修复上游新增计划文件后导致失败问题:
_.github_workflows_sync-upstream-模板-修定版本2.rar
20250309模板修定版本2,文件内容:
name: Sync Upstream on: schedule: - cron: '0 0 * * *' # 每天 00:00 UTC 运行 workflow_dispatch: # 允许手动触发 jobs: sync: runs-on: ubuntu-latest steps: - name: 检出你的 Fork 仓库 uses: actions/checkout@v4 with: persist-credentials: false - name: 配置 Git 用户信息 run: | git config --global user.name "github-actions[bot]" git config --global user.email "github-actions[bot]@users.noreply.github.com" - name: 配置 GitHub 身份验证 run: | git config --global credential.helper 'store' echo "https://github-actions[bot]:${{ secrets.GITHUB_TOKEN }}@github.com" > ~/.git-credentials - name: 添加上游仓库,并保留本地 .github/workflows/ 目录 run: | git remote add upstream https://github.com/cmliu/CF-Workers-docker.io || true # 这里改为你的上游仓库 git fetch upstream git checkout main # 这里改为你的上游主要分支,一般可能是main或者master git checkout -b temp-merge # 创建一个临时分支 # 先合并上游,但跳过 .github/workflows 目录 git read-tree --prefix=.github/workflows/ -u HEAD # 备份本地 .github/workflows/ git merge -X theirs upstream/main --allow-unrelated-histories || true # 默认保留上游代码,但 .github/workflows/ 目录不会被影响。 git checkout HEAD -- .github/workflows/ # 恢复本地的 .github/workflows/ 目录,确保合并不会覆盖它 git checkout main # 回到主分支,一般可能是main或者master git reset --hard temp-merge # 硬重置到合并后的状态 git branch -D temp-merge # 删除临时分支 git add .github/workflows/ # 添加之前备份的本地 .github/workflows/ git commit -m "同步上游代码,并保留本地 .github/workflows/" || true - name: 推送到 Fork 仓库 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | git push origin main --force --no-verify # 这里改为你的上游主要分支,一般可能是main或者master