Initialize, Populate, and Post Your Repo to GitHub in a Single Step

I don’t know about you, but I’m a bit obsessed with saving time.

I enjoy sharing code on GitHub, but I get annoyed that I have to break away from the IDE or terminal to do it—navigating a browser can just feel so slow.

So I threw together this simple function that makes it fast and easy to get a project up on GitHub, and I thought I’d share it. Once you have it set-up, it’s literally a one-step deal.

Other than git, a GitHub account, and a bash terminal, the only real dependency here is hub, the excellent git command line tool.

Now, I do all of my work on a Mac, so I installed hub via homebrew, but you can peruse hub’s GitHub page for more installation options.

Once you’ve got hub installed and working, you should be able to drop something like the following code into your .bash_profile (making sure to replace the paths with ones relevant to your own development environment,) and use it to build a new repo in just one step.

The function below automatically creates a folder in my local Repos/ directory, initializes a repository inside of it, populates the repo with default README, LICENSE, and .gitignore files, and then goes on to create a remote GitHub repo, track it, and push a first commit. It finishes up by opening my browser to the GitHub repo so I can verify everything and add a description if I like—all from a single click.

function mkpub {
printf "What would you like to name your repository? "
read name
#Replace with a local path to your repos
cd '/CMiller/Dev/Repos/'
mkdir $name
cd $name
git init
#Replace with local paths to any default files you'd like to include
#Note: the -f flag forces overwrite; use -i for interactivity if you prefer
cp -f '/CMiller/Dropbox/Dev/GitInit/Public/' .
cp -f '/CMiller/Dropbox/Dev/GitInit/Public/LICENSE' .
cp -f '/CMiller/Dropbox/Dev/GitInit/.gitignore' .
hub create
git add .
git commit -m "First commit: add README, LICENSE, and .gitignore."
git push -u origin master
hub browse

Lastly, to create a function for making private repos instead of public ones, simply add a ‘-p’ flag to the ‘hub create’ line above.

I hope you find this as useful as I do.

Thanks for stopping by.