DEV Community

Cover image for Create a new GitHub Repository from the command line
Sarah Lean 🏴󠁧󠁒
Sarah Lean 🏴󠁧󠁒

Posted on

Create a new GitHub Repository from the command line

It can be frustrating when you are working on a project on your local machine and need to commit some changes to GitHub, but you haven't set up that repository.

The GitHub CLI can help in this situation, from your terminal you can create that repository and commit your project without leaving your terminal or Integrated development environment (IDE). If you haven't got the GitHub CLI installed, do check out my blog post that covers the best ways to do that.

Convert a directory to a Git repository

So, you've been working on a project on your local machine for the last few hours and you want to commit that project to a GitHub repository. The first step is to convert the directory to a Git repository. The command to use is:

git init
Enter fullscreen mode Exit fullscreen mode

Stage the files

The next step would be to stage the files so they can be committed to your GitHub repository:

git add .
Enter fullscreen mode Exit fullscreen mode

Commit the files

Now commit the files:

git commit -m "initial commit"
Enter fullscreen mode Exit fullscreen mode

Authorise GitHub CLI

If you have never used the GitHub CLI tool before, you need to authorise it with your GitHub account. If you type in the following command it will walk you through allowing your terminal to make changes to your GitHub account.

gh auth login
Enter fullscreen mode Exit fullscreen mode

Create your GitHub repository

Your directory is Git ready and your terminal is authorised to make changes to your GitHub account. The next step is to create that GitHub repository and push your files into that repository. The command you want to use is gh repo create. This command has a lot of switches you can use for different reasons.

The command below will create a repository in GitHub called "My-NewRepo". It will be a public repository. The directory you are in will be source for that GitHub repository and the push the files in the directory to it.

gh repo create my-newrepo --public --source=. --remote=upstream --push
Enter fullscreen mode Exit fullscreen mode

Your directory is now Git initiated, you have a GitHub repository to store your project and the files are stored there. 😊
Enjoy this new found joy of being able to create GitHub repositories using the GitHub CLI tool!

Command Line icon by Icons8

Oldest comments (1)

Collapse
 
andypiper profile image
Andy Piper

I'm always forgetting --remote=upstream ... I should create a shell alias!