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 jq

How it works

Clone repositories interactively with FZF

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/zsh
2
3# Check if required commands are available
4command -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 forked
9echo "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 found
14if [ -z "$repos" ]; then
15 echo "No repositories found (or all are archived/forked)"
16 exit 1
17fi
18
19# Use fzf for selection with custom display
20selected=$(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 selected
33if [ -z "$selected" ]; then
34 echo "No repositories selected."
35 exit 0
36fi
37
38# Extract URLs and clone repositories
39echo ""
40echo "Cloning selected repositories..."
41echo "$selected" | awk -F'\t' '{print $1}' | while read -r url; do
42 repo_name=$(basename "$url" .git)
43 if [ ! -d "$repo_name" ]; then
44 gh repo clone "$url"
45 echo "✓ Successfully cloned $repo_name"
46 fi
47done
48
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-repos

Then you can run clone-repos from anywhere in your terminal.

More useful functions in my dotfiles repo