Skip to content

Instantly share code, notes, and snippets.

@metaory
Last active July 24, 2025 04:23
Show Gist options
  • Select an option

  • Save metaory/dc05168913b89f8d065e87a3e976a004 to your computer and use it in GitHub Desktop.

Select an option

Save metaory/dc05168913b89f8d065e87a3e976a004 to your computer and use it in GitHub Desktop.
gcl ── A minimal bash utility for cloning git repositories with intelligent directory naming

gcl

Git Clone with Smart Naming

A minimal bash utility for cloning git repositories with intelligent directory naming


Installation

# Clone the repo
git clone git@gist.github.com:dc05168913b89f8d065e87a3e976a004.git

# Navigate to repo
cd dc05168913b89f8d065e87a3e976a004

# Give execution permissions
chmod +x gcl

# Link it somewhere in your PATH
ln -svf $PWD/gcl /usr/bin/gcl

# Use it anywhere
gcl git@github.com:metaory/markup.json.git +u

Usage

gcl <SSH_URL> [OPTION]

Examples

Basic Clone

gcl git@github.com:metaory/markup.json.git
# Clones to: markup.json

Username Prefix

gcl git@github.com:metaory/markup.json.git -u
# Clones to: metorial__markup.json

Username Suffix

gcl git@github.com:metaory/markup.json.git +u
# Clones to: markup.json__metorial

Custom Suffix Word

gcl git@github.com:metaory/markup.json.git +foobar
# Clones to: markup.json__foobar

Custom Prefix Word

gcl git@github.com:metaory/markup.json.git -foobar
# Clones to: foobar__markup.json

Language Suffix

gcl git@github.com:metaory/markup.json.git +l
# Clones to: markup.json__javascript

Language Prefix

gcl git@github.com:metaory/markup.json.git -l
# Clones to: javascript__markup.json

Custom Suffix Sentence

gcl git@github.com:metaory/markup.json.git x11 xorg
# Clones to: markup.json__x11-xorg

Options

  • -u or -U: Prefix with username
  • +u or +U: Suffix with username
  • -l or -L: Prefix with language
  • +l or +L: Suffix with language
  • +<name>: Suffix with custom word
  • -<name>: Prefix with custom word
  • x12 xog: Suffix with hyphen joined sentence

Features

  • Extracts username and repo name from SSH URLs
  • Uses double underscore (__) as separator
  • Clones with --depth 1 for shallow clones
  • Includes --recurse-submodules for submodule support
  • Robust error handling with bash strict mode
  • Colored output for debugging

Installation

chmod +x gcl
# Add to PATH or use directly

License

MIT

#!/bin/bash
trap exit INT
set -o errexit
set -o errtrace
set -o pipefail
set -o noclobber
shopt -s extglob
help() {
cat <<EOF
Usage: gcll <SSH_URL> [OPTION]
Examples:
gcll git@github.com:metaory/markup.json.git
gcll git@github.com:metaory/markup.json.git -u
gcll git@github.com:metaory/markup.json.git +u
gcll git@github.com:metaory/markup.json.git +foobar
gcll git@github.com:metaory/markup.json.git -foobar
Options:
-u, -U Prefix with username
+u, +U Suffix with username
-l, -L Prefix with language
+l, +L Suffix with language
+<name> Suffix with custom word
-<name> Prefix with custom word
x12 xog Suffix with underscore joined sentence
-h, --help Show this help
EOF
}
[[ "$1" == "-h" || "$1" == "--help" ]] && {
help
exit 0
}
[ -z "$1" ] && {
echo "Error: SSH URL required" >&2
help >&2
exit 1
}
[[ "$1" =~ ^git@github\.com:[^/]+/[^/]+\.git$ ]] || {
echo "Error: Invalid SSH URL format. Expected: git@github.com:user/repo.git" >&2
exit 1
}
URL="$1"
DIR="$REP"
read -r USR REP < <(sed -E 's|git@github.com:([^/]*)/(.*)\.git|\1 \2|' <<<"$URL")
read -r LNG < <(gh repo view --json primaryLanguage --jq '.primaryLanguage.name // "NA"' "$URL")
case "${2:-}" in
[+][[:alpha:]][[:alnum:]-]*) DIR="${REP}__${2:1}" ;;
[-][[:alpha:]][[:alnum:]-]*) DIR="${2:1}__${REP}" ;;
-u | -U) DIR="${USR}__${REP}" ;;
+u | +U) DIR="${REP}__${USR}" ;;
-l | -L) DIR="${LNG}__${REP}" ;;
+l | +L) DIR="${REP}__${LNG}" ;;
*) ETC="${*:2}"; DIR="${REP}__${ETC// /-}" ;;
esac
DIR="${DIR,,}"
[ -d "$DIR" ] && {
echo "Error: Directory '$DIR' already exists" >&2
exit 1
}
log() {
while [ "$1" ]; do
echo -e "$1 [\e[33m${!1}\e[0m]"
shift
done
}
log URL LNG USR REP DIR
git clone \
--depth 1 \
--recurse-submodules \
"${URL}" "${DIR}"
# vim: ft=bash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment