Created
August 3, 2025 18:56
-
-
Save teebow1e/96c009e21432a34f8b943929ae965d7f to your computer and use it in GitHub Desktop.
Mirror any repo into a private repository on your GitHub account
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
#!/bin/bash | |
#=============================================================================== | |
# mirror-repo.sh | |
# | |
# Clones a source repository and mirrors it to a new private repository on | |
# your GitHub account. | |
# | |
# Usage: | |
# ./mirror-repo.sh <source_repository_url> | |
# | |
# Example: | |
# ./mirror-repo.sh https://github.com/git/git.git | |
# | |
# Dependencies: | |
# - git | |
# - GitHub CLI (gh) | |
#=============================================================================== | |
set -e | |
set -o pipefail | |
if [[ -z "$1" ]]; then | |
echo "❌ Error: No source repository URL provided." | |
echo "Usage: $0 <source_repository_url>" | |
exit 1 | |
fi | |
SOURCE_REPO_URL="$1" | |
DEST_REPO_NAME=$(basename -s .git "$SOURCE_REPO_URL") | |
BARE_CLONE_DIR="${DEST_REPO_NAME}.git" | |
if ! command -v git &> /dev/null; then | |
echo "❌ Error: 'git' is not installed. Please install it and ensure it's in your PATH." | |
exit 1 | |
fi | |
if ! command -v gh &> /dev/null; then | |
echo "❌ Error: GitHub CLI ('gh') is not installed. Please install it to continue." | |
exit 1 | |
fi | |
if ! gh auth status &> /dev/null; then | |
echo "❌ Error: You are not logged into the GitHub CLI. Please run 'gh auth login' first." | |
exit 1 | |
fi | |
echo | |
echo "🔎 Checking if repository '$DEST_REPO_NAME' already exists on your GitHub account..." | |
if gh repo view "$DEST_REPO_NAME" &>/dev/null; then | |
echo "❌ Error: A repository named '$DEST_REPO_NAME' already exists on your GitHub account." | |
echo "Please choose a different name, or delete the existing repository and try again." | |
exit 1 | |
fi | |
echo "✅ Repo name $DEST_REPO_NAME available. Proceeding..." | |
echo | |
TEMP_DIR=$(mktemp -d) | |
trap 'rm -rf "$TEMP_DIR"' EXIT | |
cd "$TEMP_DIR" | |
echo "📂 Cloning '$SOURCE_REPO_URL' as a bare repository..." | |
git clone --bare "$SOURCE_REPO_URL" "$BARE_CLONE_DIR" | |
echo "✅ Clone complete." | |
cd "$BARE_CLONE_DIR" | |
echo "🔧 Removing original 'origin' remote to prevent conflict..." | |
git remote remove origin | |
echo "🚀 Creating private repository '$DEST_REPO_NAME' on GitHub and pushing the mirror..." | |
gh repo create "$DEST_REPO_NAME" --private --description "Private mirror of $SOURCE_REPO_URL" --source=. --push | |
echo "🎉 Success! Your private mirror has been created on GitHub." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment