Created
October 31, 2023 19:03
-
-
Save guim4dev/2add1ed794c1dbd39c5ac543bad15949 to your computer and use it in GitHub Desktop.
Auto Reclone repos inside current folder
This file contains 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 | |
# Specify the directory where your Git repositories are located | |
repo_dir="." | |
# Function to delete a Git repository if it exists | |
delete_repo() { | |
local repo_dir="$1" | |
if [ -d "$repo_dir" ]; then | |
echo "Deleting existing repository: $repo_dir" | |
rm -rf "$repo_dir" | |
fi | |
} | |
# Function to clone a Git repository | |
clone_repo() { | |
local repo_url="$1" | |
echo "Cloning repository: $repo_url" | |
git clone "$repo_url" "$repo_dir/$(basename "$repo_url" .git)" | |
} | |
# Loop through all directories in the repo_dir | |
for repo_path in "$repo_dir"/*; do | |
if [ -d "$repo_path" ] && [ -d "$repo_path/.git" ]; then | |
# Extract the repository URL from the .git/config file | |
repo_url=$(git -C "$repo_path" remote get-url origin) | |
if [ -n "$repo_url" ]; then | |
delete_repo "$repo_path" | |
clone_repo "$repo_url" | |
else | |
echo "Failed to extract the repository URL for: $repo_path" | |
fi | |
fi | |
done | |
echo "Repository management completed." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment