Clone repositories interactively with FZF
Clone repositories interactively with FZF
Recently I got a new MacBook and needed to clone quite a lot of public and private repositories. Manually cloning each one was tedious, so I created this script to simplify the process using GitHub CLI and FZF.
Prerequisites
Install the required tools:
1brew install gh fzf jqHow it works
The script fetches your GitHub repositories, filters out archived and forked repos, and presents them in a searchable FZF interface. You can select multiple repos using TAB and clone them all at once.
Code example
1#!/bin/zsh2
3# Check if required commands are available4command -v gh >/dev/null 2>&1 || { echo "Error: gh CLI is not installed"; exit 1; }5command -v fzf >/dev/null 2>&1 || { echo "Error: fzf is not installed"; exit 1; }6command -v jq >/dev/null 2>&1 || { echo "Error: jq is not installed"; exit 1; }7
8# Fetch repos, filter out archived and forked9echo "Fetching repositories..."10repos=$(gh repo list --limit 1000 --json name,url,description,isArchived,isFork | \11 jq -r '.[] | select(.isArchived == false and .isFork == false) | "\(.url)\t\(.description // "No description")"')12
13# Check if any repos were found14if [ -z "$repos" ]; then15 echo "No repositories found (or all are archived/forked)"16 exit 117fi18
19# Use fzf for selection with custom display20selected=$(echo "$repos" | \21 fzf --multi \22 --delimiter='\t' \23 --with-nth=1 \24 --preview 'echo {2}' \25 --preview-window=right:30%:wrap \26 --header="Select repos to clone (TAB to select multiple, ENTER to confirm)" \27 --prompt="Repos > " \28 --height=100% \29 --layout=reverse \30 --border)31
32# Exit if no repos are selected33if [ -z "$selected" ]; then34 echo "No repositories selected."35 exit 036fi37
38# Extract URLs and clone repositories39echo ""40echo "Cloning selected repositories..."41echo "$selected" | awk -F'\t' '{print $1}' | while read -r url; do42 repo_name=$(basename "$url" .git)43 if [ ! -d "$repo_name" ]; then44 gh repo clone "$url"45 echo "✓ Successfully cloned $repo_name"46 fi47done48
49echo ""50echo "Done!"Key features
- Multi-selection: Use TAB to select multiple repositories
- Live preview: See repository descriptions in the preview pane
- Smart filtering: Automatically excludes archived and forked repositories
- Safety checks: Skips cloning if directory already exists
Usage
To use this script, save the code above to a file (e.g., clone-repos.sh), make it executable with chmod +x clone-repos.sh, and run it with ./clone-repos.sh.
For global access, create a symlink:
1sudo ln -s "$(pwd)/clone-repos.sh" /usr/local/bin/clone-reposThen you can run clone-repos from anywhere in your terminal.
More useful functions in my dotfiles repo