DEV Community

Cover image for Ok Boomer! Instant GitHub Repo Creation in One Command 🚀
Sam
Sam

Posted on

Ok Boomer! Instant GitHub Repo Creation in One Command 🚀

Boom Live Action GIFs 🤩

On an existing folder/repo

Existing Repo

Creating a new folder/repo

New repo/folder

Quick Explanation / Installation

Hey DEV community! If you're anything like me, you frequently start new projects to experiment with ideas, and find yourself needing to manually create new repositories on GitHub for backups.

If so, you'll appreciate this little boom script I wrote a few years back. Simply copy/paste the code snippet below into your .rc/.bashrc/.zshrc file and you're ready to boom 🤯

👉 Note: This script uses the GitHub CLI. So make sure you've installed that if you haven't already. Instructions here.

#!/bin/bash
# Function to quiet the output of a cmd unless it fails 

# Optional function I find handy for silencing the output of commands
quiet () {
    "$@" > /dev/null 2>&1
}

boom () {
    echo "Boom! Creating Git & GitHub repo. Please wait..."
        # If a param is passed, create a dir with that name and cd to it
    if [ $1 ]
    then
        quiet mkdir $1 && cd $1
        echo "Created folder $1"
    fi
        # Create README file if it doesn't already exist with directory name
    if [ ! -f README.md ]
    then
        quiet echo "# ${PWD##*/}" > README.md
        echo "Generated a README.md file"
    fi
    git init
    git add .
    git commit -m "Initial commit"
    gh repo create ${PWD##*/} --private --source=. --remote=origin
    git push -u origin main
    echo "Boom! Your repo is live: $(git config --get remote.origin.url)"
}

Enter fullscreen mode Exit fullscreen mode

How's it work? 🤔

The boom script simplifies repository creation into one straightforward command. Here's how you can use it:

With Argument:

Running the script with an argument creates a new folder, navigates into it, generates a README.md file with the folder name as the title, initializes a new Git repository, adds all files, commits the changes, pushes the new repository to GitHub, and outputs the repository URL.

Without Argument:

If run without an argument, the script generates a README.md file with the current folder name as the title, initializes a new Git repository, adds all files, commits the changes, pushes the new repository to GitHub, and outputs the repository URL.

Notes

  • Customize your repository by adding a .gitignore file before running the script if you don't want to include all your files.
  • By default, the script creates private GitHub repositories. Adjust this behavior by modifying the script. Repositories can be deleted from GitHub using the command hub delete -y repo-name.

Ok Boomer 👴🏻

Hope this saves you all some time! I've used variants of this script myself hundreds of times for over a decade now!

If you're interested in other things I've made to speed up our dev lives check out the other post I wrote here recently about Maintenance.dev, a new side project I launched that lets you get an instantly toggle-able customizable maintenance page for your own website with a single <script>, so you don't have to do any more manual deploys just to toggle maintenance pages.

Happy coding everyone! 🫡

Top comments (2)

Collapse
 
2kabhishek profile image
Abhishek Keshri

I've been actually using something for a couple of years that also sets up a default template of your choice, you can find it here.

github.com/2kabhishek/mkrepo

Collapse
 
tofudotdev profile image
Sam

Hey @2kabhishek, you're right that's very similar! Their templating idea is a nice touch too! Snazzy! 🤩

I did think about wrapping this up as a homebrew cask too, but decided I'd just keep it light and leave it up to people to customize as they like.