Create a new GitHub Repository from the command line
In this blog post I show you how to create a new GitHub repository from the command line. I explain how the gh repo create command.
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. All without leaving your terminal or Integrated Development Environment (IDE). If you haven't got the GitHub CLI installed, 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
Stage the files
The next step would be to stage the files so they can be committed to your GitHub repository:
git add .
Commit the files
The next step is to commit the files:
git commit -m "initial commit"
Authorise GitHub CLI
If you have never used the GitHub CLI tool before, you must authorise it with your GitHub account. If you type in the following command, it will walk you through allowing your terminal to change your GitHub account.
gh auth login
Create your GitHub repository
Your directory is Git ready, and your terminal is authorised to change 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 the 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
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