Last active
May 14, 2017 12:06
-
-
Save suewonjp/32af159a63378b8bea42f737507cd441 to your computer and use it in GitHub Desktop.
This file contains 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 | |
## Preconditions; | |
## 1: Destination (Github) repository name == Source (local) repository name | |
## 2: [IMPORTANT!!!] Those two repositories (for GitHub & local) should be created prior to running this script | |
scriptName=${0##*/} | |
if [ $# -lt 1 ] ; then | |
echo "Usage : $scriptName [repo name] [github user name] [remote alias]" | |
exit 0 | |
fi | |
srcRepoPath="${1}" | |
userName="${2:-suewonjp}" ## Customize default value as you wish | |
remoteAlias="${3:-github}" ## Customize default value as you wish | |
echo "$scriptName : srcRepoPath=${srcRepoPath}" | |
echo "$scriptName : userName=${userName}" | |
echo "$scriptName : remoteAlias=${remoteAlias}" | |
## Of course, the source repository should exist! | |
[ -d "${srcRepoPath}" ] && { | |
cd "${srcRepoPath}" | |
srcRepoPath="$PWD" | |
[ -d .git ] || { | |
echo "$scriptName [ERROR] : The directory [ ${srcRepoPath} ] is NOT a git repository" | |
exit 1 | |
} | |
cd - > /dev/null 2>&1 | |
} || { | |
echo "$scriptName [ERROR] : The directory [ ${srcRepoPath} ] doesn't exist!" | |
exit 1 | |
} | |
srcRepo="${srcRepoPath##*/}" | |
dstRepo="https://github.com/${userName}/${srcRepo}.git" | |
type wget || { | |
echo "$scriptName [ERROR] : wget is required! Please, install it first" | |
exit 1 | |
} | |
wget --spider "${dstRepo}" || { | |
echo "$scriptName [ERROR] : The remote repository [ ${dstRepo} ] doesn't exist!" | |
exit 1 | |
} | |
## Create a temporary directory for a bare clone to push | |
tmpDir=~/Downloads/__tmp__$( date "+%y-%m-%d_%Hh%Mm%Ss" ) | |
mkdir -p "${tmpDir}" | |
trap 'rm -rf "${tmpDir}"' 0 | |
cd "${tmpDir}" | |
## Create a bare clone of the source repository | |
git clone --bare "${srcRepoPath}" | |
## Push the bare repository to GitHub using the "mirror" option, | |
## which ensures that all references, such as branches and tags are copied to the destination repository | |
cd "${srcRepo}".git | |
git push --mirror "${dstRepo}" | |
## Move to the source repository | |
cd "${srcRepoPath}" | |
## Add the destination Github repository as remote | |
git remote add "${remoteAlias}" "${dstRepo}" | |
## Confirm the remote has been properly added | |
git remote -v | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
say that you have a local git repo named MyAweomeProject
First, manually create your repo at github ( the name of the github repo should be MyAweomeProject )
Run the script
git-export-repo-to-github.sh /path/to/MyAwesomeProject