Skip to content

Instantly share code, notes, and snippets.

@davegreenwood
Created June 27, 2020 12:50
Show Gist options
  • Save davegreenwood/469a31ec578bef655bce51fda34a059f to your computer and use it in GitHub Desktop.
Save davegreenwood/469a31ec578bef655bce51fda34a059f to your computer and use it in GitHub Desktop.
A script to initialise a git repo locally and GitHub.
#!/bin/bash
# Make executable with chmod +x <<filename.sh>>
# based on:
# https://www.freecodecamp.org/news/automate-project-github-setup-mac/
#
# but using a github personal access token:
# https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
echo "Create a new github repository in a subdirectory within the current directory."
echo ""
echo "Makes a new directory with the repo name, adds a README.md and .gitignore."
echo "The repo is commited to GitHub using the Personal Access Token (PAT)"
echo "in the environment variable GITHUB_TOKEN that has previously been created."
echo "and the github username environment variable GITHUB_USER."
echo "The repo is set to private, as a safer default."
echo ""
# name of the remote repo. Enter a SINGLE WORD ..or...separate with hyphens
echo "What name do you want to give your remote repo?"
read REPO_NAME
echo "Enter a repo description: "
read DESCRIPTION
# make the repo directory and cd
mkdir $REPO_NAME
cd $REPO_NAME
# initialise the repo locally, create blank README, add and commit
git init
touch README.md
touch .gitignore
git add README.md
git add .gitignore
git commit -m 'initial commit -setup with .sh script'
# use github API to log the user in and create remote
curl -u ${GITHUB_USER}:${GITHUB_TOKEN} https://api.github.com/user/repos \
-d "{\"name\": \"${REPO_NAME}\", \"description\": \"${DESCRIPTION}\", \"private\": true}"
# add the remote github repo to local repo and push
git remote add origin https://github.com/${GITHUB_USER}/${REPO_NAME}.git
git push --set-upstream origin master
echo "Done. Go to https://github.com/$GITHUB_USER/$REPO_NAME to see."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment