This is a Bash script that takes the name of a local Git repo and a remote server host/path, generates a bare repo, and copies the bare repo to a Git server located in another server.
Example usage:
centralize-git-repo.sh arepo user@gitserver:/srv/git
It generates a folder in server:/srv/git/arepo.git
and adds the remote in the local repo.
#!/bin/bash
set -e
GIT="/usr/bin/git"
SCP="/usr/bin/scp"
if [ "$1" == "--help" ] | [ $# -eq 0 ] ; then
echo "$0 [repo] [user@]hostname:[remote_git_path]"
exit 0
fi
# Assign parameters
LOCAL_REPO=$(basename $1)
REMOTE_DIR=$2
# Checks if the repo exists in current directory.
if [ ! -d "${LOCAL_REPO}" ]; then
echo "${LOCAL_REPO} is not a repo in current directory."
exit 1
fi
# Checks second parameter is present
if [ ! "${REMOTE_DIR}" ]; then
echo "Remote is missing, pass the target dir on the remote "
echo " server as the second parameter as: user@server:/dir/../git/"
echo ""
echo "e.g.: $0 localrepo user@gitserver:/srv/git"
exit 1
fi
######
# Creates a container for the bare repo
BARE_TMP_DIR=$(mktemp --quiet --directory)
NEW_REPO_DIRNAME=${LOCAL_REPO}.git
BARE_REPO_PATH=${BARE_TMP_DIR}/${NEW_REPO_DIRNAME}
mkdir ${BARE_REPO_PATH}
$GIT clone --bare ${LOCAL_REPO} ${BARE_REPO_PATH}/
$SCP -r ${BARE_REPO_PATH} ${REMOTE_DIR}
# add remote
cd ${LOCAL_REPO}
$GIT remote add origin ${REMOTE_DIR}/${NEW_REPO_DIRNAME}
$GIT remote -v
# clean
rm -rf ${BARE_TMP_DIR}
cd ..
git push
. It would be very similar to this process anyway: - Initialize a bare repo in the remote server, add the remote locally and push. \$\endgroup\$