DEV Community

Cover image for Initialize, Populate, and Post Your Git Repo to GitHub in a Single Step
Cary Miller
Cary Miller

Posted on

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

I don't know about you, but I'm a bit obsessed with saving time... in fact, I'm pretty sure I put more energy into saving time than I do working on my code (oh man, that's a discussion for another day.)

Anyway, I like having my code up 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 SLOOOOW.

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.

I work on a Mac, so I installed hub via homebrew$ brew install hub—but you can peruse their 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/README.md' .
    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
}
Enter fullscreen mode Exit fullscreen mode

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

I hope you find this useful.

Thanks for stopping by!

Top comments (2)

Collapse
 
agc93 profile image
Alistair Chapman

Nice post!

If you're willing to also depend on curl you could replace your local copy of .gitignore using GitHub's repo, so something like the following:

STACK="Scala" # as an example
curl https://raw.githubusercontent.com/github/gitignore/master/$STACK.gitignore -o .gitignore

For npm lovers, there's also utilities to download gitignore files and license files

Collapse
 
ben profile image
Ben Halpern

Thanks, super useful!