DEV Community

Cover image for How to Create a GitHub Repository from your Terminal
Muhammad Arif Raza
Muhammad Arif Raza

Posted on

How to Create a GitHub Repository from your Terminal

šŸš€ How to Create a GitHub Repository from Your Terminal
GitHub is a developerā€™s best friend when it comes to version control and collaboration. While most people use the GitHub web interface to create repositories, did you know you can do it faster directly from your terminal? Whether you're a seasoned developer or just getting started, this guide will show you how to create a GitHub repository straight from the command line using the GitHub CLI.

šŸ›  Prerequisites
Before diving in, make sure you have the following tools ready to go:

Git Installed

Check if Git is installed:

bash
Copy code
git --version
Enter fullscreen mode Exit fullscreen mode

If not, download and install it from git-scm.com.
GitHub CLI (gh) Installed

Download the GitHub CLI from cli.github.com.
After installation, verify it by running:

bash
Copy code
gh --version
Enter fullscreen mode Exit fullscreen mode

A GitHub Account

Ensure you have an active GitHub account.
šŸš¦ Step-by-Step Guide

  1. Authenticate with the GitHub CLI Log in to GitHub from your terminal:
bash
Copy code
gh auth login
Follow the on-screen instructions to authenticate via your browser or terminal.
Enter fullscreen mode Exit fullscreen mode
  1. Navigate to Your Project Directory If you already have a project folder, move into it:
bash
Copy code
cd /path/to/your/project
If not, create a new directory:
Enter fullscreen mode Exit fullscreen mode
bash
Copy code
mkdir my-project  
cd my-project 
Enter fullscreen mode Exit fullscreen mode
  1. Initialize Git If you havenā€™t initialized a Git repository in your project folder, do so now:
bash
Copy code
git init 
Enter fullscreen mode Exit fullscreen mode
  1. Create the GitHub Repository Use the GitHub CLI to create a new repository:
bash
Copy code
gh repo create <repository-name>
Enter fullscreen mode Exit fullscreen mode

Replace with your desired repo name. Youā€™ll be prompted to select:

Visibility: Public or Private
Location: In the current directory or as a blank repo
For example, to create a public repository in your current folder, run:

bash
Copy code
gh repo create my-awesome-project --public
Enter fullscreen mode Exit fullscreen mode
  1. Stage and Commit Your Files Stage all files for commit:
bash
Copy code
git add . 
Enter fullscreen mode Exit fullscreen mode

Create your first commit:

bash
Copy code
git commit -m "Initial commit"  
Enter fullscreen mode Exit fullscreen mode
  1. Push Your Code to GitHub Push your local code to the newly created remote repository:
bash
Copy code
git branch -M main  
git push -u origin main  
Enter fullscreen mode Exit fullscreen mode

āœ… Verify Your Repository
Head over to GitHub and check your profile or organization to see your new repository live!

šŸ’” Bonus Tips
To create repositories for a specific GitHub organization, use:

bash
Copy code
gh repo create <repository-name> --org <organization-name> 
Enter fullscreen mode Exit fullscreen mode

Use gh repo view to quickly view repository details from your terminal.

Automate your workflow by combining these steps into a shell script.

šŸš€ Conclusion
Creating a GitHub repository from your terminal is quick, efficient, and keeps you in your flow. Tools like the GitHub CLI make managing repositories a breeze. Whether youā€™re starting a new project or migrating an existing one, this method saves time and keeps things simple.

Do you have other tips for managing GitHub repositories? Let me know in the comments below!

Happy coding! šŸŽ‰

Top comments (0)