Skip to content

Instantly share code, notes, and snippets.

@rchapin
Created January 2, 2021 23:10
Show Gist options
  • Save rchapin/1adaf7e2708f0f7e2606be24ae07dd14 to your computer and use it in GitHub Desktop.
Save rchapin/1adaf7e2708f0f7e2606be24ae07dd14 to your computer and use it in GitHub Desktop.
Bash script for updating the remote URL for multiple repos
#!/bin/bash
# #############################################################################
# If you typically keep your cloned repos in a single directory and happen
# to move the location of your remote, here is a script for quickly updating
# the remote url for all of the cloned repos in a given directory.
#
# Run from the same directory in which all of the repos reside as follows:
#
# /update-remote-url.sh "dir1 dir2 dir3" "[email protected]:/path/to/parent/dir"
#
# Running as follows will update the remote for "dir1" to
#
# [email protected]:/path/to/parent/dir/dir1.git
#
# #############################################################################
# CONFIGS
#
# If your remote has a different name simply export this env var before
# running this script setting it to a different string.
#
export REMOTE_NAME=${REMOTE_NAME:-origin}
#
# #############################################################################
set -u
REPO_DIRS=$1
URL=$2
echo "Updating remote urls for origin to $URL"
for r in $REPO_DIRS
do
start_dir=$(pwd)
echo "---------"
echo "Updating remote for repo $r"
cd $r
echo "Current remote:"
git remote -v
repo_name=$(git remote -v | head -n 1 | awk '{print $2}' | awk -F\/ '{print $NF}')
new_url="$URL/$repo_name"
echo -n "Updating remote to:$new_url"
git remote set-url origin $new_url
echo
echo "New remote:"
git remote -v
cd $start_dir
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment