Skip to content

Instantly share code, notes, and snippets.

@tlercher
Created July 4, 2017 14:10
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 tlercher/4c510a961d10e512ba9fc3f398caff95 to your computer and use it in GitHub Desktop.
Save tlercher/4c510a961d10e512ba9fc3f398caff95 to your computer and use it in GitHub Desktop.
Migrate git repoistories
#!/bin/bash
old_url="ssh://git@old.git.host:4711/"
new_url="ssh://git@new.git.host:4711/"
working_directory=$(pwd)
for dir in $(find $working_directory -maxdepth 1 -type d \( ! -name . \) -print); do
is_using_git=$( { git -C $dir rev-parse --is-inside-work-tree; } 2>&1 )
if [ "$is_using_git" == "true" ]; then
src_url=$(git -C $dir remote get-url origin)
dst_url=$(echo $src_url | sed "s,$old_url,$new_url,g")
if [ "$src_url" == "$dst_url" ]; then
echo "Repo '$(basename $dir)' already migrated ...SKIPPING"
echo ""
continue
fi
echo "Migrating '$(basename $dir)'..."
echo "FROM $src_url"
echo "TO: $dst_url"
read -r -p "Change URL? [Y/n] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]] || [ -z $response ]; then
git -C $dir remote set-url origin $dst_url
echo "... DONE"
else
echo "... SKIP"
fi
echo ""
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment