Skip to content

Instantly share code, notes, and snippets.

@melMass
Last active December 10, 2025 11:47
Show Gist options
  • Select an option

  • Save melMass/b11d700f3f52b349ce15f322751ad116 to your computer and use it in GitHub Desktop.

Select an option

Save melMass/b11d700f3f52b349ce15f322751ad116 to your computer and use it in GitHub Desktop.

Barestrap

curl -sL https://gist.githubusercontent.com/melMass/b11d700f3f52b349ce15f322751ad116/raw/42c39e16dfbe6fd8ab27aaec3e2e0f171e83ca2d/barestrape.sh | sh -s -- git@github.com:org/repo.git
#!/bin/sh
# Barestrap - clones a bare repo and checks out all branches as worktrees
set -e
# Usage: sh barestrap.sh <repo_url> [target_dir]
# Example: curl -sL https://gist.githubusercontent.com/melMass/b11d700f3f52b349ce15f322751ad116/raw/42c39e16dfbe6fd8ab27aaec3e2e0f171e83ca2d/barestrape.sh | sh -s -- git@github.com:org/repo.git
REPO_URL="${1:-}"
TARGET_DIR="${2:-}"
if [ -z "$REPO_URL" ]; then
echo "Usage: sh bootstrap.sh <repo_url> [target_dir]"
echo "Example: sh bootstrap.sh https://github.com/user/repo"
exit 1
fi
if [ -z "$TARGET_DIR" ]; then
TARGET_DIR=$(basename "$REPO_URL" .git)
fi
BARE_DIR="$TARGET_DIR/.bare"
if [ -d "$BARE_DIR" ]; then
echo "Error: $BARE_DIR already exists"
exit 1
fi
echo "==> Cloning bare repository into $TARGET_DIR..."
mkdir -p "$TARGET_DIR"
git clone --bare "$REPO_URL" "$BARE_DIR"
echo "==> Configuring remote refs..."
git -C "$BARE_DIR" config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git -C "$BARE_DIR" fetch origin
echo "==> Checking out all branches..."
branches=$(git --git-dir="$BARE_DIR" for-each-ref --format='%(refname:short)' refs/remotes/origin | grep -v '/HEAD$')
for branch in $branches; do
local_name="${branch#origin/}"
worktree_path="$TARGET_DIR/$local_name"
if [ -d "$worktree_path" ]; then
echo " Skipping $local_name (directory exists)"
continue
fi
echo " Checking out $local_name..."
if git --git-dir="$BARE_DIR" show-ref --verify --quiet "refs/heads/$local_name" 2>/dev/null; then
git --git-dir="$BARE_DIR" worktree add "$worktree_path" "$local_name"
else
git --git-dir="$BARE_DIR" worktree add -b "$local_name" "$worktree_path" "$branch"
fi
if [ -f "$worktree_path/.gitmodules" ]; then
echo " Initializing submodules for $local_name..."
git -C "$worktree_path" submodule update --init --recursive
fi
done
echo ""
echo "==> Done! Worktrees created in $TARGET_DIR/"
echo ""
ls -1 "$TARGET_DIR" | grep -v '\.bare$' | sed 's/^/ /'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment