Created
June 28, 2022 03:06
-
-
Save AndrewKvalheim/552c0481421616017919550f45faf51b to your computer and use it in GitHub Desktop.
Configure GitHub remotes for HTTPS fetch and SSH push
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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="[email protected]:$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