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.

Revisions

  1. JBlond created this gist Jul 7, 2025.
    59 changes: 59 additions & 0 deletions git-change-protocol
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,59 @@
    #!/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