Created
December 5, 2025 09:32
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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." |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Required action secrets: