Skip to content

Instantly share code, notes, and snippets.

@urwrstkn8mare
Created June 24, 2021 10:20
Show Gist options
  • Save urwrstkn8mare/ddaf22076c52ca4646eae2c99dae7bc8 to your computer and use it in GitHub Desktop.
Save urwrstkn8mare/ddaf22076c52ca4646eae2c99dae7bc8 to your computer and use it in GitHub Desktop.
A bash script to convert git repos in a directory of folders to use SSH from HTTPS. (1 folder depth)
#/bin/bash
for d in */ ; do
cd "$d"
inside_git_repo="$(git rev-parse --is-inside-work-tree 2>/dev/null)"
if [ "$inside_git_repo" ]; then
repo_url=`git remote -v | grep -m1 '^origin' | sed -Ene's#.*(https://[^[:space:]]*).*#\1#p'`
if [ -z "$repo_url" ]; then
echo "$d - Could not identify repo url (might already be using SSH)."
else
user=`echo $repo_url | sed -Ene's#https://github.com/([^/]*)/(.*).git#\1#p'`
if [ -z "$user" ]; then
echo "$d - Could not identify user."
else
repo=`echo $repo_url | sed -Ene's#https://github.com/([^/]*)/(.*).git#\2#p'`
if [ -z "$repo" ]; then
echo "$d - Could not identify repo."
else
new_url="git@github.com:$user/$repo.git"
`git remote set-url origin $new_url`
echo "$d - Converted git repo to use: $new_url (SSH)"
fi
fi
fi
else
echo "$d - Not a git repository."
fi
cd ".."
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment