Skip to content

Instantly share code, notes, and snippets.

@monkpit
Created January 18, 2022 17:28
Show Gist options
  • Save monkpit/8959b3d9e2bfd89cd1dd0b788912c81c to your computer and use it in GitHub Desktop.
Save monkpit/8959b3d9e2bfd89cd1dd0b788912c81c to your computer and use it in GitHub Desktop.
Change git remote in all subdirectories
#! /usr/bin/env bash
# if all subfolders are JS projects with a package.json containing repository.url field
# and repository.url is something like "ssh://git@server/project" or "ssh://
REMOTE_PREFIX=<your git server prefix like git@server>
for repo in */; do
(
cd "$repo" || exit
PROJECT_NAME=$(jq -r '.repository.url' package.json | sed -r 's/.*:\/\///' | sed -r 's/[^/]*(.*)\.git/\1/')
echo "$PROJECT_NAME"
set -x
echo git remote set-url origin "$REMOTE_PREFIX$PROJECT_NAME.git"
# uncomment this when ready
# git remote set-url origin "$REMOTE_PREFIX$PROJECT_NAME.git"
set +x
)
done;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment