Skip to content

Instantly share code, notes, and snippets.

@BHznJNs
Created December 5, 2025 09:32
Show Gist options
  • Select an option

  • Save BHznJNs/e691d52d9994825678f96af273de67bc to your computer and use it in GitHub Desktop.

Select an option

Save BHznJNs/e691d52d9994825678f96af273de67bc to your computer and use it in GitHub Desktop.
A GitHub Action script that can sync a private repo of one user to another repo of another user.
name: Sync Private Repository
on:
workflow_dispatch:
jobs:
mirror-sync:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Mirror Sync Operation
run: |
SOURCE_URL="https://${{ secrets.SOURCE_USERNAME }}:${{ secrets.SOURCE_PAT }}@github.com/${{ secrets.SOURCE_USERNAME }}/${{ secrets.SOURCE_REPO_NAME }}.git"
TARGET_URL="https://${{ github.actor }}:${{ secrets.TARGET_PAT }}@github.com/${{ github.actor }}/${{ secrets.TARGET_REPO_NAME }}.git"
echo "Starting sync from ${{ secrets.SOURCE_USERNAME }}/${{ secrets.SOURCE_REPO_NAME }} to ${{ secrets.TARGET_REPO_NAME }}..."
# 创建临时目录用于克隆裸仓库 (Bare Clone),--bare 意味着我们只克隆 .git 数据,不检出具体文件
git clone --bare "$SOURCE_URL" temp_mirror_dir
# 进入目录并推送到目标
cd temp_mirror_dir
# 这是一个危险操作:--mirror 会强制覆盖目标仓库的所有分支和标签,使其与源仓库完全一致
git push --mirror "$TARGET_URL"
# 清理
cd ..
rm -rf temp_mirror_dir
echo "Sync completed successfully."
@BHznJNs
Copy link
Author

BHznJNs commented Dec 5, 2025

Required action secrets:

  • SOURCE_PAT: The PAT from source user, requires repo access permission
  • SOURCE_REPO_NAME: Source repo name
  • SOURCE_USERNAME: Source user name
  • TARGET_PAT: The PAT from target user, requires repo access permission and workflow access permission
  • TARGET_REPO_NAME: Target repo name

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment