Skip to content

Instantly share code, notes, and snippets.

@NorkzYT
Created January 23, 2024 11:17
Show Gist options
  • Save NorkzYT/fd24dbcd96a4cb2dbca3704d9b9410e4 to your computer and use it in GitHub Desktop.
Save NorkzYT/fd24dbcd96a4cb2dbca3704d9b9410e4 to your computer and use it in GitHub Desktop.
Git Mirror Script
#!/bin/bash
# This script is designed to add a target remote to your Git repository and then
# force-push all branches and tags to this target. It's particularly useful for
# mirroring or backing up repositories. The script prompts for the target's username,
# personal access token, and repository URL. Be cautious with force-pushing as it
# can overwrite changes in the target repository.
# Bash script to add a remote and force-push all branches and tags
# Prompt for target username
read -p "Enter the username for the target repository: " INPUT_TARGET_USERNAME
# Prompt for target token
# Note: It's generally unsafe to enter tokens directly. Consider reading from a file or environment variable in a secure setup.
read -s -p "Enter the personal access token for the target repository: " INPUT_TARGET_TOKEN
echo
# Prompt for target URL
read -p "Enter the target repository URL (https://github.com/username/repo): " INPUT_TARGET_URL
# Add the remote target
git remote add target https://${INPUT_TARGET_USERNAME}:${INPUT_TARGET_TOKEN}@${INPUT_TARGET_URL#https://}
# Check if remote add was successful
if [ $? -eq 0 ]; then
echo "Remote 'target' added successfully."
else
echo "Failed to add remote 'target'. Exiting script."
exit 1
fi
# Force push all branches to the target
echo "Pushing all branches to the target..."
git push -f --all target
# Force push all tags to the target
echo "Pushing all tags to the target..."
git push -f --tags target
echo "Script completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment