Mirroring Git repo between providers

Mon, 14 January 2019

git
git_mirror.sh
#!/usr/bin/env sh

#
# Usage:
# /path/to/git_mirror.sh <source git url> <target git url>
#
DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
cd ${DIR}
GIT_PATH="/usr/bin/env git"
IFS='/' read -ra PATH <<< "${1}"
for i in "${PATH[@]}"; do
    if [[ "${i}" != *"@"* ]]; then
        DIRECTORY="${i}"
        `${GIT_PATH} clone --mirror "${1}" "${DIRECTORY}"`
        `cd "${DIR}/${DIRECTORY}" && ${GIT_PATH} remote set-url origin "${2}" && ${GIT_PATH} push --force`
    fi
done
git