How to Mirror a Git Repo

TDLR; Use the --mirror flag to git push to a new origin

It's often useful to create a mirror of your Git repos, in order to have your code backed up on your own servers or an external servoce

If you don't already have it locally, clone the source repo
git clone --mirror https://gitsite.com/lissy93/original-repository.git

Navigate into it
cd original-repository

Finally, use the --mirror flag to copy everything in your local Git repository into the new repo.
git push --mirror https://gitsite.com/lissy93/new-repository.git

To clone all repositories for a given user or organisation, simply run:

curl -s https://api.github.com/users/${user}/repos?per_page=200 | grep \"clone_url\" | awk '{print $2}' | sed -e 's/"//g' -e 's/,//g' | xargs -n1 git clone

Where ${user} is your username/ org name, and if you have over 200 projects, then increase per_page as required


You can also use GitHub Actions to automatically sync your repo to another Git platform.

name: Mirror to Codeberg
on:
  push:
    branches: [ 'main' ]    
  workflow_dispatch: # Manual dispatch
  schedule:
    - cron: '0 5 * * 6'
jobs:
  codeberg:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - uses: pixta-dev/repository-mirroring-action@v1
        with:
          target_repo_url: "git@codeberg.org:alicia/awesome-privacy.git"
          ssh_private_key: ${{ secrets.CODEBERG_SSH }}