Skip to content

Instantly share code, notes, and snippets.

@hackerb9
Created August 20, 2021 05:34
Show Gist options
  • Save hackerb9/080ad30a2fe17324ae390a3fadb362b5 to your computer and use it in GitHub Desktop.
Save hackerb9/080ad30a2fe17324ae390a3fadb362b5 to your computer and use it in GitHub Desktop.
Fix existing github repository to use ssh access instead of username/password
#!/bin/bash
# A temporary script for a temporary problem.
# I have a bunch of github repositories that no longer work
# for git push because the original URL uses the deprecated
# username/password combo. This script repairs the remote
# origin so it uses ssh.
# B9 August 2021
oldurl=$(git remote get-url origin)
if [[ -z "$oldurl" ]]; then
echo "gitfixssh error: Git can't get the remote origin URL." >&2
exit 1
fi
if [[ "$oldurl" == [email protected]:* ]]; then
echo "gitfixssh: Repository already set to ssh access." >&2
exit 0
fi
if [[ "$oldurl" != http*github.com/* ]]; then
echo "gitfixssh error: Unrecognized remote origin url: '$oldurl'." >&2
exit 1
fi
[email protected]:${oldurl#*github.com/}
echo "Old origin URL: '$oldurl'"
echo "New origin URL: '$url'"
echo "git remote set-url origin '$url'"
git remote set-url origin "$url"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment