-
-
Save m14t/3056747 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 | |
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" |
@ptanmay143 Thanks for the fix for the typo bug. I've updated my comment as well.
Awesome! Made my life so much easier.
Hi folks! FYI
git
supports this as a configurable nowadays:git config --global url."[email protected]:".insteadOf "https://github.com/"
Thank you so much
Nice work! Thanks!
To note, @m14t is simply running one command here, but is gathering useful information from the 30 lines above. If you know the SSH URL, you can simple run,
git remote set-url origin [email protected]:username/repo-name-here.git
Where
username
is the username of the repo owner andrepo-name-here
is the name of that user's repository.The URL can be found in the repositories homepage in this box,
I hope that helps!
Thanks for the help !
Thank you so much guys!
Point to be noted is that if you use Xcode with Swift Package Manager then this is going to cause issues for you as Xcode by default doesn't respect this , and continues to use its own way of doing things. It is a really irritating bug. You can check this SO question for more details
@brettalton thanks for the one liner!
Hi, in case someone need to get git remote url as https
, whatever your remote Git repository settings are.
more at https://gist.github.com/CodeIter/be215cfef4ec1af5e1a10c4563944869
[ -z "${GITHOST}" ] && GITHOST="github.com"
[ -z "${GITBRANCH}" ] && GITBRANCH="origin"
git remote -v \
| grep "${GITBRANCH}" | grep fetch | head -n 1 \
| sed -re 's~\s+\(fetch\)$~~' \
-re 's~([^/ :]+/[^/ :]+)$~#\1~;s~^[^#]+#+~~' \
-re 's~(github\.com)~\1/~' \
-re '/^https?:\/\//!s~^~https://'"${GITHOST}"'/~'
If your https url include user:password, like https://user:[email protected]/username/reponame
, then you should use this:
REPO_URL=$(git remote -v | grep -m1 '^origin' | sed -Ene's#.*(https://[^[:space:]]*).*#\1#p')
echo "$REPO_URL"
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"
eval "$CHANGE_CMD"
echo "Success"
Fixed an error on line 5. Unexpected ; after else.