Skip to content

Instantly share code, notes, and snippets.

@slhck
Created June 24, 2025 08:31
Show Gist options
  • Save slhck/e94a8061101f2f69f22cb016d53ff876 to your computer and use it in GitHub Desktop.
Save slhck/e94a8061101f2f69f22cb016d53ff876 to your computer and use it in GitHub Desktop.
Convert Git repos from Git SSH to HTTPS
#!/bin/bash
# This script converts all git remote URLs from SSH to HTTPS for all repositories in the current directory.
#
# Author: Werner Robitza
#
# Usage:
# ./convert_to_https.sh
TARGET_PATH="."
GIT_HOSTNAME="git.example.com" # Change this to your Git hostname
DRY_RUN=
usage() {
echo "Usage: $0 [-n] [-h] [TARGET_PATH]"
echo
echo " TARGET_PATH: The path to the repositories to convert, if unset, the current directory is used"
echo
echo "Options:"
echo " -n, --dry-run: Dry run (do not make any changes)"
echo " -h, --help: Show this help message"
exit 1
}
while [ $# -gt 0 ]; do
case "$1" in
-n|--dry-run)
DRY_RUN=1
shift
;;
-h|--help)
usage
;;
*)
TARGET_PATH="$1"
shift
;;
esac
done
if [ -n "$TARGET_PATH" ]; then
SEARCH_PATH="$TARGET_PATH"
else
SEARCH_PATH="."
fi
find "$SEARCH_PATH" -type d -name ".git" | while read -r gitdir; do
repo="${gitdir%/.git}"
cd "$repo" || continue
for remote in $(git remote); do
url=$(git remote get-url "$remote")
if [[ "$url" =~ ^git@(git\.)?${GIT_HOSTNAME}:(.+)$ ]]; then
https_url="https://${GIT_HOSTNAME}/${BASH_REMATCH[2]}"
if [ -z "$DRY_RUN" ]; then
git remote set-url "$remote" "$https_url"
echo -e "\033[32mConverted $url to $https_url\033[0m"
else
echo -e "\033[33mWould convert $url to $https_url\033[0m"
fi
else
echo -e "\033[33mSkipping $url\033[0m"
fi
done
git config -f .gitmodules --get-regexp '^submodule\..*\.url$' 2>/dev/null | while read -r key value; do
if [[ "$value" =~ ^git@(git\.)?${GIT_HOSTNAME}:(.+)$ ]]; then
https_url="https://${GIT_HOSTNAME}/${BASH_REMATCH[2]}"
if [ -z "$DRY_RUN" ]; then
git config -f .gitmodules "$key" "$https_url"
echo -e "\033[32mConverted $value to $https_url\033[0m"
else
echo -e "\033[33mWould convert $value to $https_url\033[0m"
fi
fi
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment