Skip to content

Instantly share code, notes, and snippets.

@JBlond
Created July 7, 2025 09:39
Show Gist options
  • Save JBlond/aa36f11da413caad1d2e5dc7b9176a8a to your computer and use it in GitHub Desktop.
Save JBlond/aa36f11da413caad1d2e5dc7b9176a8a to your computer and use it in GitHub Desktop.
git-change-protocol
#!/usr/bin/env bash
# Get current remote URL
currentURL=$(git config --get remote.origin.url)
# Exit early if no remote URL is set
if [[ -z "$currentURL" ]]; then
echo "No remote.origin.url is set. Nothing to do."
exit 0
fi
# Detect SSH format (e.g. [email protected]:user/repo.git)
if [[ "$currentURL" == git@*:* ]]; then
# Extract host and path
host="${currentURL#git@}"
host="${host%%:*}"
path="${currentURL#*:}"
# Remove trailing .git if present
path="${path%.git}"
newURL="https://$host/$path"
# Preserve .git if it was present
[[ "$currentURL" == *.git ]] && newURL+=".git"
direction="SSH ➝ HTTPS"
# Detect HTTPS format (e.g. https://github.com/user/repo.git)
elif [[ "$currentURL" == http*://*/* ]]; then
# Strip protocol
tmp="${currentURL#http://}"
tmp="${tmp#https://}"
host="${tmp%%/*}"
path="${tmp#*/}"
# Remove trailing .git if present
path="${path%.git}"
newURL="git@$host:$path"
# Preserve .git if it was present
[[ "$currentURL" == *.git ]] && newURL+=".git"
direction="HTTPS ➝ SSH"
else
echo "The current remote URL format is not recognized: $currentURL"
exit 1
fi
# Confirm and apply
echo "Current URL: $currentURL"
echo "Target format: $direction"
echo "New URL: $newURL"
read -p "Do you want to apply this change? (y/n): " response
if [[ "$response" == "y" ]]; then
git remote set-url origin "$newURL"
echo "Git remote updated."
else
echo "Git remote unchanged."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment