Skip to content

Instantly share code, notes, and snippets.

@nyrahul
Last active December 22, 2020 06:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nyrahul/0dd05f6379c993d8089b66f3ab7f0461 to your computer and use it in GitHub Desktop.
Save nyrahul/0dd05f6379c993d8089b66f3ab7f0461 to your computer and use it in GitHub Desktop.
Switch all the remotes from https to git
#!/bin/bash
# Switch all the remotes from https to git
# e.g, https://github.com/username/reponame.git -> git@github.com:username/reponame.git
switch4remote()
{
url=`git remote get-url $1`
[[ ! $url =~ ^https ]] && echo "Remote [$1] might be on git already" && return 0
path=`echo $url | sed -Ene 's#https://github.com/(.*)#\1#p'`
[[ "$path" == "" ]] && echo "couldnot identify the path! URL:$url" && return 1
cmd="git remote set-url $1 git@github.com:$path"
echo "$cmd"
$cmd
}
# for all remotes: origin, upstream, ...
for remote in `git remote -v | awk '{ print $1 }' | uniq`; do
switch4remote $remote
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment