Created
January 3, 2018 19:33
-
-
Save ivan-pinatti/c4b2d4f879a27468000a448b30f77081 to your computer and use it in GitHub Desktop.
Git - Migrate to new remote - #git #migrate
This file contains hidden or 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
#!/usr/bin/env bash | |
: ' Move Git repository from an old remote to a new one | |
' | |
# check if debug flag is set | |
if [ "${DEBUG}" = true ]; then | |
set -x # enable print commands and their arguments as they are executed. | |
export # show all declared variables (includes system variables) | |
whoami # print current user | |
else | |
# unset if flag is not set | |
unset DEBUG | |
fi | |
# bash default parameters | |
set -o errexit # make your script exit when a command fails | |
set -o pipefail # exit status of the last command that threw a non-zero exit code is returned | |
set -o nounset # exit when your script tries to use undeclared variables | |
# check binaries | |
__GIT=$(which git) | |
# parameters | |
__repository_folder="${1:-}" | |
__git_new_remote="${2:-}" | |
# function | |
function __print_usage() { | |
echo -e "Error!\n\nUsage is "${0}" <REPOSITORY_FOLDER> <NEW_REMOTE>" | |
exit 1 | |
} | |
# check parameters | |
if [ ! ${__repository_folder} ] || [ ! ${__git_new_remote} ]; then | |
__print_usage | |
fi | |
# unshallow old origin | |
${__GIT} -C "${__repository_folder}" fetch --unshallow origin | |
# create new origin | |
${__GIT} -C "${__repository_folder}" remote add new-origin "${__git_new_remote}" | |
# push current content to new origin | |
${__GIT} -C "${__repository_folder}" push --all new-origin | |
${__GIT} -C "${__repository_folder}" push --tags new-origin | |
# remove old origin | |
${__GIT} -C "${__repository_folder}" remote rm origin | |
# rename new-origin to origin | |
${__GIT} -C "${__repository_folder}" remote rename new-origin origin |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment