Skip to content

Instantly share code, notes, and snippets.

@yogeek
Created December 21, 2017 09:11
Show Gist options
  • Select an option

  • Save yogeek/db04dc59675c2b7f94b78e7818121ee4 to your computer and use it in GitHub Desktop.

Select an option

Save yogeek/db04dc59675c2b7f94b78e7818121ee4 to your computer and use it in GitHub Desktop.
GIT
# Script to switch between HTTP and SSH git remote URL
gitswitch () {
if [[ "$(git remote -v)" == "" ]]
then
echo "No remote configured. Use 'git remote add' to set one"
return
fi
remoteRoot=$(git remote -v | head -1 | grep root)
if [[ "$remoteRoot" == "" ]]
then
echo "git switching : https -> ssh"
repo=$(git remote -v | head -1 | sed -Ene's#.*https://([^[:space:]|/]*)/([^[:space:]]*).*#\1:\2#p')
prefix="root@"
else
echo "git switching : ssh -> https"
repo=$(git remote -v | head -1 | sed -Ene's#.*root@([^[:space:]]*):([^[:space:]]*).*#\1/\2#p')
prefix="https://"
fi
git remote set-url origin $prefix$repo
git remote -v
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment