Skip to content

Instantly share code, notes, and snippets.

@jasonm23
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonm23/e036fa05d6c62a489c59 to your computer and use it in GitHub Desktop.
Save jasonm23/e036fa05d6c62a489c59 to your computer and use it in GitHub Desktop.
Github create repo from the terminal. see comment for usage
github-create-repo() {
w="\033[37m\033[1m"
rst="\033[0m"
org_name=$1
repo_name=$2
__add_all(){
printf "${w}Adding all changes / new files to index and committing.${rst}\n"
git add -A
git status -s
printf "Use default commit message? ${w}\"Initialize Repo\"${rst} [N/y]\n"
read confirm_default_commit_message
if [ "$confirm_default_commit_message" = "y" ]; then
git commit -m "Initialize Repo"
else
git commit
fi
}
__push_local(){
printf "Pushing local code to remote ...\n"
git remote add origin git@github.com:${repo_short}.git
git push -u origin master
printf "Done.\n"
}
__create_repo(){
route=$1
repo_name=$2
repo_short=$3
curl -u "$username:$token" https://api.github.com/${route}/repos \
-d '{"name":"'$repo_name'", "homepage": "https://github.com/'$repo_short'"}'
}
__write_readme(){
printf "# ${repo_name}\n\nTODO: Add a proper README.md\n\n## Description\n\n## Usage\n\n## Licence\n\n## Contributing" > README.md
}
dir_name=`basename $(pwd)`
invalid_credentials=0
if [[ -d ".git" ]]; then
printf "Found local git repo...\n"
else
printf "Not in a git repo...\n"
printf "Would you like to initialize the current folder ${w}`pwd`${rst} as a git repo? [N/y]\n"
read confirm_init
if [ "$confirm_init" = "y" ];then
git init
else
exit
fi
fi
if [ "$org_name" = "" ];then
printf "Create github repo for organisation?\n"
printf "(or leave blank to add repo to your user accout): "
read org_name
fi
if [ "$org_name" != "" ];then
printf "You are creating a github repo for this organisation: ${w}${org_name}${rst} \n"
fi
if [ "$repo_name" = "" ]; then
printf "Github Repo name (hit enter to use ${w}'${dir_name}'${rst})?\n"
read repo_name
fi
if [ "$repo_name" = "" ]; then
repo_name=$dir_name
fi
username=`git config github.user`
if [ "$username" = "" ]; then
printf "Could not find username, run ${w}'git config --global github.user <username>'${rst}\n"
invalid_credentials=1
fi
token=`git config github.token`
if [ "$token" = "" ]; then
printf "Could not find token, run ${w}'git config --global github.token <token>'${rst}\n"
invalid_credentials=1
fi
if [ "$invalid_credentials" -eq "1" ]; then
return 1
fi
printf "Creating Github repository ${w}'${repo_name}'${rst} ...\n"
if [ "$org_name" = "" ];then
repo_short="${username}/${repo_name}"
__create_repo "user" "$repo_name" "$repo_short"
else
repo_short="${org_name}/${repo_name}"
__create_repo "orgs/${org_name}" "$repo_name" "$repo_short"
fi
printf "Done.\n"
if [ ! -f README.md ]; then
printf "No README found...\n"
printf "Use a placeholder README.md? [N/y]\n"
read confirm_placeholder
if [ "$confirm_placeholder" = "y" ];then
__write_readme
cat README.md
printf "Use this? [Y/n]\n"
read readme_ok
if [ "$readme_ok" = "N" ];then
rm README.md
printf "Deleted README.md\n"
fi
fi
fi
printf "Add / Commit all new files and modification in the local repo? [N/y]\n"
read confirm_add_all
if [ "$confirm_add_all" = "y" ]; then
__add_all
fi
printf "Push local repo to remote? [N/y]\n"
read confirm_push
if [ "$confirm_push" = "y" ]; then
__push_local
fi
}
@jasonm23
Copy link
Author

Install

To install the bash function, download the script copy it to /usr/local/bin and add the following line to your bashrc or zshrc.

source /usr/local/bin/github-create-repo.sh

Usage

From a git repo folder: (or even from an un-init'd folder you'd like to be a repo.)

github-create-repo

You will be prompted for several options.

github-create-repo $org-name $repo-name

To specify the organisation name (if you're using one) and set the repo name (if different from the current folder name.)

Git config github.username and github.token must be set in ~/.gitconfig

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