odex25_standard/checkout_branches_full_name...

43 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
# Prune worktree references to remove any missing worktrees
git worktree prune
# Function to add worktree and check out branch
add_worktree_and_checkout() {
local branch=$1
# Create the directory if it doesn't exist
mkdir -p "../${branch}"
# Add the worktree
git worktree add "../${branch}" "origin/${branch}" || {
echo "Failed to add worktree for branch ${branch}"
return 1
}
# Change to the new worktree directory and check out the branch
cd "../${branch}"
git checkout "${branch}" || {
echo "Failed to checkout branch ${branch} in directory ${branch}"
return 1
}
cd - > /dev/null
}
# List remote branches that start with 'origin/dev'
for branch in $(git branch -r | grep 'origin/dev' | sed 's|origin/||'); do
add_worktree_and_checkout "${branch}"
done
# List all branches that start with "preprod" and check out each branch into a separate directory
for branch in $(git branch -r | grep 'origin/preprod' | sed 's|origin/||'); do
add_worktree_and_checkout "${branch}"
done
# List all branches that start with "master" and check out each branch into a separate directory
for branch in $(git branch -r | grep 'origin/master' | grep -vE 'origin/(HEAD|master$)' | sed 's|origin/||'); do
add_worktree_and_checkout "${branch}"
done