Skip to content

Instantly share code, notes, and snippets.

@APratham
Last active May 16, 2024 18:30
Show Gist options
  • Save APratham/0a98a4bc2541c6affdd59c4439faea46 to your computer and use it in GitHub Desktop.
Save APratham/0a98a4bc2541c6affdd59c4439faea46 to your computer and use it in GitHub Desktop.
Script to download all branches of a repository into different folders
<#
.SYNOPSIS
This script checks out all branches of a Git repository into separate folders.
.DESCRIPTION
To run this script, follow these steps:
1. Open PowerShell as the current user.
2. Temporarily set the execution policy for the current session to Unrestricted by running:
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Scope Process
3. Run the script with the repository URL as a parameter:
.\checkout_branches.ps1 -RepoUrl <repository-url>
.NOTES
Changing the execution policy to Unrestricted allows all scripts to run in the current session. This is less secure but necessary for running unsigned scripts downloaded from the internet.
#>
param (
[string]$repositoryUrl
)
if (-not $repositoryUrl) {
Write-Host "Error: No repository URL provided." -ForegroundColor Red
Write-Host ""
Write-Host "Usage: .\checkout.ps1 <repository-url>"
Write-Host ""
Write-Host "This script clones a repository and checks out all branches using git worktree."
Write-Host ""
Write-Host "Options:"
Write-Host " -h, --help Show this help message and exit"
exit 1
}
if (-not $repositoryUrl) {
Write-Host "Usage: .\checkout.ps1 <repository-url>"
exit 1
}
Set-Location ..
if (-not (Test-Path -Path "repos" -PathType Container)) {
New-Item -Path "repos" -ItemType Directory
}
Set-Location "repos"
$repoUrl = $repositoryUrl
$repoName = [System.IO.Path]::GetFileNameWithoutExtension($repoUrl)
git clone --mirror $repoUrl
Set-Location "$repoName.git"
git fetch --all
$branches = git for-each-ref --format='%(refname:short)' refs/heads/
foreach ($branch in $branches) {
$folderName = "${branch}_branch"
git worktree add ("../" + $folderName) $branch
}
Set-Location "..\.."
Write-Host "All branches have been checked out into their respective directories."
#!/bin/bash
# Make the script executable
# chmod +x checkout.sh
# Provide repository URL as a parameter while executing
# ./checkout.sh <repository-url>
show_help() {
echo "Usage: $0 <repository-url>"
echo
echo "This script clones a repository and checks out all branches into different folders using git worktree."
echo
echo "Options:"
echo " -h, --help Show this help message and exit"
}
if [[ "$1" == "-h" || "$1" == "--help" ]]; then
show_help
exit 0
fi
if [ -z "$1" ]; then
echo -e "\033[31mError: No repository URL provided.\033[0m"
echo
show_help
exit 1
fi
cd ..
mkdir repos && cd repos
REPO_URL=$1
BASE_DIR=$(pwd)
git clone --mirror "$REPO_URL"
cd $(basename "$REPO_URL" .git).git
git fetch --all
BRANCHES=$(git for-each-ref --format='%(refname:short)' refs/heads/)
for BRANCH in $BRANCHES; do
FOLDER_NAME="${BRANCH}_branch"
git worktree add "../$FOLDER_NAME" "$BRANCH"
done
cd "$BASE_DIR"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment