Skip to content

Instantly share code, notes, and snippets.

@Cyclodex
Created December 29, 2021 10:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cyclodex/332802d133b67a379fcd4e9a5841a69a to your computer and use it in GitHub Desktop.
Save Cyclodex/332802d133b67a379fcd4e9a5841a69a to your computer and use it in GitHub Desktop.
Collects all git repositories and creates a clone script out of it
$FILENAME = "clone-all-repos.ps1"
Write-Output "# extracted git clone commands" | Out-File -FilePath "$FILENAME"
Get-ChildItem . -Attributes Directory+Hidden -ErrorAction SilentlyContinue -Filter ".git" -Recurse | ForEach-Object {
$DIR_NAME = $_.Parent.Name
$DIR = Resolve-Path -Path $_.Parent.FullName -Relative
$GIT_DIR = "$DIR/.git"
Write-Output "" | Out-File -FilePath "$FILENAME" -Append
Write-Output "# $DIR_NAME" | Out-File -FilePath "$FILENAME" -Append
# Creates the folder structure
Write-Output "mkdir $DIR" | Out-File -FilePath "$FILENAME" -Append
# Clones the repository in the same folder
Write-Output "git clone $(git --git-dir=$GIT_DIR remote get-url origin) $dir;" | Out-File -FilePath "$FILENAME" -Append
# Add other remotes
$(git --git-dir=$GIT_DIR remote | Select-String -NotMatch "origin") | ForEach-Object {
Write-Output "git --git-dir=$GIT_DIR remote add $_ $(git --git-dir=$GIT_DIR remote get-url $_);" | Out-File -FilePath "$FILENAME" -Append
}
}
@Cyclodex
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment