Skip to content

Instantly share code, notes, and snippets.

@AndrewKvalheim
Created June 28, 2022 03:06
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 AndrewKvalheim/552c0481421616017919550f45faf51b to your computer and use it in GitHub Desktop.
Save AndrewKvalheim/552c0481421616017919550f45faf51b to your computer and use it in GitHub Desktop.
Configure GitHub remotes for HTTPS fetch and SSH push
#!/usr/bin/env bash
set -Eeuo pipefail
if [[ ! ( "${1-}" =~ ^([-_[:alnum:]]+)/([-_.[:alnum:]]+)$ ) ]]; then
echo "Usage: ${0##*/} <owner>/<repo>" >&2
exit 1
fi
owner="${BASH_REMATCH[1]}"
repo="${BASH_REMATCH[2]}"
https="https://github.com/$owner/$repo.git"
ssh="git@github.com:$owner/$repo.git"
if git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
if git remote | grep --quiet "^$owner$"; then
echo "Configuring GitHub remote $owner/$repo for HTTPS fetch and SSH push" >&2
git remote set-url "$owner" "$https"
git remote set-url --push "$owner" "$ssh"
else
echo "Adding GitHub remote $owner/$repo with HTTPS fetch and SSH push" >&2
git remote add -f "$owner" "$https"
git remote set-url --push "$owner" "$ssh"
fi
else
echo "Cloning from GitHub remote $owner/$repo with HTTPS fetch and SSH push" >&2
git clone --origin "$owner" "$https" "$repo"
cd "$repo"
git remote set-url --push "$owner" "$ssh"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment