Skip to content

Instantly share code, notes, and snippets.

@kujiy
Created May 20, 2026 06:54
Show Gist options
  • Select an option

  • Save kujiy/3df008de4606119e498dcf8fcc0ee7bb to your computer and use it in GitHub Desktop.

Select an option

Save kujiy/3df008de4606119e498dcf8fcc0ee7bb to your computer and use it in GitHub Desktop.
A Bash script to change all Git URLs in ~/repos/*/.git/config from HTTPS to SSH
#!/usr/bin/env bash
set -euo pipefail
find ~/repos -type f -path '*/.git/config' | while read -r config; do
echo "Processing: $config"
# remote.origin.url を取得
current_url=$(git config --file "$config" --get remote.origin.url || true)
if [[ -z "$current_url" ]]; then
echo " -> skip (no remote.origin.url)"
continue
fi
# https://github.com/user/repo.git
# ↓
# git@github.com:user/repo.git
if [[ "$current_url" =~ ^https://([^/]+)/(.+)$ ]]; then
host="${BASH_REMATCH[1]}"
path="${BASH_REMATCH[2]}"
ssh_url="git@${host}:${path}"
echo " $current_url"
echo " -> $ssh_url"
git config --file "$config" remote.origin.url "$ssh_url"
else
echo " -> skip (not https)"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment