Skip to content

Instantly share code, notes, and snippets.

@costa86
Last active December 1, 2021 13:04
Show Gist options
  • Save costa86/472edcbb933844ec98aa070ab90c49e6 to your computer and use it in GitHub Desktop.
Save costa86/472edcbb933844ec98aa070ab90c49e6 to your computer and use it in GitHub Desktop.
Create repository using a script [requires SSH access]
TOKEN=<github_token>
USERNAME=<github_user>
FILENAME=automate_git.sh
echo "What name do you want to give your remote repo? "
read REPO_NAME
echo "Enter a repo description. Do NOT use spaces: "
read DESCRIPTION
echo "what is the absolute path to your local project directory?: "
read PROJECT_PATH
# replace potencial undesired spaces by -
REPO_NAME="${REPO_NAME//+( )/-}"
DESCRIPTION="${DESCRIPTION//+( )/-}"
# replace potential undesired spaces by /
PROJECT_PATH="${PROJECT_PATH//+( )//}"
cd "$PROJECT_PATH"
# sudo remove .git folder if it exists in project folder
GIT_FOLDER=.git
if [ -d "$GIT_FOLDER" ]; then
sudo rm -r ${GIT_FOLDER}
fi
# initialise the repo locally, create blank README, add and commit
git init
echo ${FILENAME} >> .gitignore
touch README.md
git add .
git commit -m 'initial commit -setup with .sh script'
# use github API to log the user in
curl -u tsgriff:${TOKEN} https://api.github.com/user/repos -d "{\"name\": \"${REPO_NAME}\", \"description\": \"${DESCRIPTION}\"}"
# add the remote github repo to local repo and push
git remote add origin git@github.com:${USERNAME}/${REPO_NAME}.git
git push --set-upstream origin master
echo "Done! Visit https://github.com/$USERNAME/$REPO_NAME to see it."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment