-
-
Save gatlinnewhouse/f0d8a60bfc7b8fb003a522a96e80a756 to your computer and use it in GitHub Desktop.
Convert all GitLab git remotes to SSH or HTTPS.
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
#/bin/bash | |
#-- Script to automatically convert all git remotes to HTTPS from SSH for gitlab repos | |
# Script will change all the git remotes. | |
# If you didn't intend to do that, run the other script in this repo. | |
# Original 1: https://gist.github.com/m14t/3056747 | |
# Original 2: https://gist.github.com/chuckbjones/9dc6634fe52e56ba45ac | |
# Thanks to @m14t, @michaelsilver and @chuckbjones. | |
ssh_to_https(){ | |
REPO_URL=`git remote -v | grep -m1 "^$1" | sed -Ene's#.*(git@gitlab.com:[^[:space:]]*).*#\1#p'` | |
if [ -z "$REPO_URL" ]; then | |
echo "-- ERROR: Could not identify Repo url." | |
echo " It is possible this repo is already using HTTPS instead of SSH." | |
exit | |
fi | |
USER=`echo $REPO_URL | sed -Ene's#git@gitlab.com:([^/]*)/(.*).git#\1#p'` | |
if [ -z "$USER" ]; then | |
echo "-- ERROR: Could not identify User." | |
exit | |
fi | |
REPO=`echo $REPO_URL | sed -Ene's#git@gitlab.com:([^/]*)/(.*).git#\2#p'` | |
if [ -z "$REPO" ]; then | |
echo "-- ERROR: Could not identify Repo." | |
exit | |
fi | |
#NEW_URL="git@github.com:$USER/$REPO.git" | |
NEW_URL="https://gitlab.com/$USER/$REPO.git" | |
echo "Changing repo url from " | |
echo " '$REPO_URL'" | |
echo " to " | |
echo " '$NEW_URL'" | |
echo "" | |
CHANGE_CMD="git remote set-url $1 $NEW_URL" | |
echo "$CHANGE_CMD" | |
`$CHANGE_CMD` | |
} | |
for i in `git remote`; do | |
ssh_to_https "$i"; | |
done; | |
echo "Moved everything to HTTPS!" |
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
#/bin/bash | |
#-- Script to automatically convert all git remotes to SSH from HTTPS for gitlab repos | |
# Script will change all the git remotes. | |
# If you didn't intend to do that, run the other script in this repo. | |
# Original 1: https://gist.github.com/m14t/3056747 | |
# Original 2: https://gist.github.com/chuckbjones/9dc6634fe52e56ba45ac | |
# Thanks to @m14t, @michaelsilver and @chuckbjones. | |
http_to_ssh(){ | |
echo "" | |
echo "Checking for $1..." | |
REPO_URL=`git remote -v | grep -m1 "^$1" | sed -Ene's#.*(https://[^[:space:]]*).*#\1#p'` | |
if [ -z "$REPO_URL" ]; then | |
if [ "$1" == "upstream" ]; then | |
echo "-- No upstream found" | |
exit | |
else | |
echo "-- ERROR: Could not identify Repo url." | |
echo " It is possible this repo is already using SSH instead of HTTPS." | |
exit | |
fi | |
fi | |
USER=`echo $REPO_URL | sed -Ene's#https://gitlab.com/([^/]*)/(.*).git#\1#p'` | |
if [ -z "$USER" ]; then | |
echo "-- ERROR: Could not identify User." | |
exit | |
fi | |
REPO=`echo $REPO_URL | sed -Ene's#https://gitlab.com/([^/]*)/(.*).git#\2#p'` | |
if [ -z "$REPO" ]; then | |
echo "-- ERROR: Could not identify Repo." | |
exit | |
fi | |
NEW_URL="git@gitlab.com:$USER/$REPO.git" | |
echo "Changing repo url from " | |
echo " '$REPO_URL'" | |
echo " to " | |
echo " '$NEW_URL'" | |
echo "" | |
CHANGE_CMD="git remote set-url $1 $NEW_URL" | |
echo "$CHANGE_CMD" | |
`$CHANGE_CMD` | |
} | |
for i in `git remote`; do | |
http_to_ssh "$i" | |
done; | |
echo "Moved everything to SSH!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment