Skip to content

Instantly share code, notes, and snippets.

@YellowPig-zp
Forked from m14t/fix_github_https_repo.sh
Created April 27, 2021 07:58
Show Gist options
  • Save YellowPig-zp/6a4e7dae7cd3324c3032aedb67ee5990 to your computer and use it in GitHub Desktop.
Save YellowPig-zp/6a4e7dae7cd3324c3032aedb67ee5990 to your computer and use it in GitHub Desktop.

Revisions

  1. @m14t m14t created this gist Jul 5, 2012.
    33 changes: 33 additions & 0 deletions fix_github_https_repo.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    #/bin/bash
    #-- Script to automate https://help.github.com/articles/why-is-git-always-asking-for-my-password

    REPO_URL=`git remote -v | grep -m1 '^origin' | sed -Ene's#.*(https://[^[:space:]]*).*#\1#p'`
    if [ -z "$REPO_URL" ]; then
    echo "-- ERROR: Could not identify Repo url."
    echo " It is possible this repo is already using SSH instead of HTTPS."
    exit
    fi

    USER=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*).git#\1#p'`
    if [ -z "$USER" ]; then
    echo "-- ERROR: Could not identify User."
    exit
    fi

    REPO=`echo $REPO_URL | sed -Ene's#https://github.com/([^/]*)/(.*).git#\2#p'`
    if [ -z "$REPO" ]; then
    echo "-- ERROR: Could not identify Repo."
    exit
    fi

    NEW_URL="[email protected]:$USER/$REPO.git"
    echo "Changing repo url from "
    echo " '$REPO_URL'"
    echo " to "
    echo " '$NEW_URL'"
    echo ""

    CHANGE_CMD="git remote set-url origin $NEW_URL"
    `$CHANGE_CMD`

    echo "Success"