Script to Clone and Replace GitHub Repositories

This Bash script allows you to clone a source GitHub repository and replace the content of a target repository with the source. It prompts for both repository URLs, warns the user about potential data loss, and requires confirmation before proceeding with the cloning and pushing actions.
user_0659028 avatar
user_0659028
plain_text
01/02/2025 9:20 AM
1.9 KB
14
Indexable
#!/bin/bash

# Prompt user for GitHub repository URLs
echo -e "\nEnter the URL for the Source Repo:"
read source_repo_url
echo -e "Enter the URL for the Target Repo:"
read target_repo_url

# Warning before proceeding
echo -e "\nWARNING: This will FULLY REPLACE the content of the Target Repo with the Source Repo, including branches and tags. Proceeding will overwrite any existing files in the Target Repo."
echo -e "Do you want to continue? (y/n):"
read confirm

if [[ "$confirm" != "y" ]]; then
  echo "Aborted. No changes made."
  exit 0
fi

# Clone the Source Repo
echo -e "\nCloning Source Repo from $source_repo_url..."
git clone $source_repo_url
repo_name=$(basename $source_repo_url .git)
cd $repo_name

# Add Target Repo as a remote
echo -e "\nAdding Target Repo as remote..."
git remote add target-import $target_repo_url

# Push Source Repo content to Target Repo (Main branch)
echo -e "\nPushing content to Target Repo (Main branch)..."
git push target-import main --force

# Push all branches to Target Repo
echo -e "\nPushing all branches to Target Repo..."
git push target-import --all --force

# Push all tags to Target Repo
echo -e "\nPushing all tags to Target Repo..."
git push target-import --tags --force

# Show current remotes with better formatting
echo -e "\nCurrent remotes:"
echo "---------------------"
git remote -v | while read line; do echo "  $line"; done
echo "---------------------"
Editor is loading...
Leave a Comment