Skip to content

Instantly share code, notes, and snippets.

@chuckbjones
Forked from m14t/fix_github_https_repo.sh
Last active February 12, 2022 16:08
Show Gist options
  • Save chuckbjones/9dc6634fe52e56ba45ac to your computer and use it in GitHub Desktop.
Save chuckbjones/9dc6634fe52e56ba45ac to your computer and use it in GitHub Desktop.
#/bin/bash
#-- Script to automate https://help.github.com/articles/why-is-git-always-asking-for-my-password
# Forked from the original to do the opposite: Switch ssh repo urls to https
# Original here: https://gist.github.com/m14t/3056747
# Thanks to @m14t
#origin or upstream
REMOTE=${1-origin}
REPO_URL=`git remote -v | grep -m1 "^$REMOTE" | sed -Ene's#.*([email protected]:[^[:space:]]*).*#\1#p'`
if [ -z "$REPO_URL" ]; then
echo "-- ERROR: Could not identify Repo url."
echo " It is possible this repo is already using HTTPS instead of SSH."
exit
fi
USER=`echo $REPO_URL | sed -Ene's#[email protected]:([^/]*)/(.*).git#\1#p'`
if [ -z "$USER" ]; then
echo "-- ERROR: Could not identify User."
exit
fi
REPO=`echo $REPO_URL | sed -Ene's#[email protected]:([^/]*)/(.*).git#\2#p'`
if [ -z "$REPO" ]; then
echo "-- ERROR: Could not identify Repo."
exit
fi
#NEW_URL="[email protected]:$USER/$REPO.git"
NEW_URL="https://github.com/$USER/$REPO.git"
echo "Changing repo url from "
echo " '$REPO_URL'"
echo " to "
echo " '$NEW_URL'"
echo ""
CHANGE_CMD="git remote set-url $REMOTE $NEW_URL"
echo "$CHANGE_CMD"
`$CHANGE_CMD`
echo "Success"
@n-eq
Copy link

n-eq commented Apr 18, 2017

Hi, I was wondering if we couldn't replace
REPO_URL=git remote -v | grep -m1 "^$REMOTE" | sed -Ene's#.*([email protected]:[^[:space:]]*).*#\1#p'
with:
REPO_URL=git config --get remote.origin.url
? What are some cases where this would not work? Thaks!

@jasonkarns
Copy link

@marrakchino not all remotes are named origin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment