Created
June 12, 2017 23:08
-
-
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
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 | |
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