Skip to content

Instantly share code, notes, and snippets.

@planetceres
Last active February 4, 2019 16:12
Show Gist options
  • Save planetceres/32ed36f6e0619242956e1b492f97df82 to your computer and use it in GitHub Desktop.
Save planetceres/32ed36f6e0619242956e1b492f97df82 to your computer and use it in GitHub Desktop.
Create new Github repo from command line
#!/bin/bash
git_create () {
# Based on https://gist.github.com/robwierzbowski/5430952/
# Get defaults
CURRENTDIR=${PWD##*/}
GITHUBUSER=$(git config github.user)
# Get user input
read -p "New Repo Name [$CURRENTDIR]: " REPONAME
REPONAME=${REPONAME:-$CURRENTDIR}
read -p "Github Username [$GITHUBUSER]: " USER
USER=${USER:-$GITHUBUSER}
read -p "Description [None]: " DESCRIPTION
read -n 1 -p "Create repo as private (y/n)? " private
echo $private
if [ "$private" != "${private#[Nn]}" ] ;then
PRIVATE=false
else
PRIVATE=true
fi
echo "Creating github.com/$USER/$REPONAME"
echo "Decription: $DESCRIPTION"
echo "Private: $PRIVATE"
echo ""
read -p "Press [Enter] to create repo.." cont
# Use Github API to create repo
curl -u $USER https://api.github.com/user/repos -d "{\"name\": \"$REPONAME\", \"description\": \"$DESCRIPTION\", \"private\": $PRIVATE, \"has_issues\": true, \"has_downloads\": true, \"has_wiki\": false}"
# Use ssh to set origin
git remote set-url origin [email protected]:$USER/$REPONAME.git
echo "Repo created successfully, and origin set to [email protected]:$USER/$REPONAME.git"
read -n 1 -p "Push local commits to remote origin (y/n)? " push
echo ""
if [ "$push" != "${push#[Nn]}" ] ;then
echo "Not pushing local commits to remote. Process complete."
else
git push --set-upstream origin master
echo "Local commits pushed to remote. Process complete."
fi
}
@planetceres
Copy link
Author

planetceres commented Feb 4, 2019

Assumes you have ssh keys set up with GitHub. If not, just replace remote with https equivalent.

To use:

  1. Paste git_create function in your ~/.bash_profile or ~/ .bashrc file
  2. source ~/.bash_profile or source ~/ .bashrc
  3. cd $HOME/<path_to_your_project>
  4. Run git_create from command line and follow the prompts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment