Skip to content

Instantly share code, notes, and snippets.

@mcquinne
Created June 12, 2017 23:08
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 mcquinne/e1ba7e3432691b1dd93871f24a5201a4 to your computer and use it in GitHub Desktop.
Save mcquinne/e1ba7e3432691b1dd93871f24a5201a4 to your computer and use it in GitHub Desktop.
Update the URLs for a given remote name in all git repos under the PWD
#!/bin/bash
if [[ $# -ne 3 ]]; then
echo "Finds and replaces patterns in remote URLs for all git repos under the PWD"
echo " Usage:"
echo " $0 remote_name old_pattern new_pattern"
echo " Example:"
echo " $0 origin myoldorg myneworg"
echo " Would find git repos under the PWD with 'myoldorg' in their URLs for"
echo " their origin remotes, and replace 'myoldorg' with 'myneworg' in each URL"
exit 1
fi
remote="$1"
oldpat="$2"
newpat="$3"
for repo in $(find . -type d -name "*.git"); do
oldurl=$(git --git-dir "$repo" remote get-url "$remote");
echo "Found [$repo] with [$remote] url [$oldurl]"
if grep -q "$oldpat" <<<$oldurl; then
newurl=$(sed -e "s/$oldpat/$newpat/g" <<<$oldurl)
echo "Setting [$repo] to [$remote] url [$newurl]"
git --git-dir "$repo" remote set-url "$remote" "$newurl"
else
echo "Repo [$repo] doesn't match [$oldpat], moving along..."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment