DEV Community

Cover image for How to Create a GitHub Repository from the Terminal Using GitHub CLI
Cristian Jonhson Alvarez
Cristian Jonhson Alvarez

Posted on

How to Create a GitHub Repository from the Terminal Using GitHub CLI

When working on a local project, we usually create the repository on GitHub first, copy the remote URL, and then connect it to our local project.

But there is a cleaner way to do it directly from the terminal using GitHub CLI.

1. Initialize Git in Your Project

Go to your project folder:

cd your-project-name
Enter fullscreen mode Exit fullscreen mode

Initialize Git:

git init
Enter fullscreen mode Exit fullscreen mode

2. Authenticate with GitHub CLI

If you have not logged in yet, run:

gh auth login
Enter fullscreen mode Exit fullscreen mode

During the process, you can choose:

GitHub.com
SSH
Login with a web browser
Enter fullscreen mode Exit fullscreen mode

3. Create the Remote Repository from Local

To create the repository on GitHub and add it as origin, run:

gh repo create repository-name --public --source=. --remote=origin
Enter fullscreen mode Exit fullscreen mode

Example:

gh repo create IA_ML_DL_python_fundamentos --public --source=. --remote=origin
Enter fullscreen mode Exit fullscreen mode

For a private repository:

gh repo create repository-name --private --source=. --remote=origin
Enter fullscreen mode Exit fullscreen mode

4. Verify the Remote

Check that origin was added correctly:

git remote -v
Enter fullscreen mode Exit fullscreen mode

You should see something like:

origin  git@github.com:username/repository-name.git (fetch)
origin  git@github.com:username/repository-name.git (push)
Enter fullscreen mode Exit fullscreen mode

5. Create the First Commit

Add your files:

git add .
Enter fullscreen mode Exit fullscreen mode

Create the first commit:

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

6. Push the Project to GitHub

Make sure your main branch is named main:

git branch -M main
Enter fullscreen mode Exit fullscreen mode

Push your project:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Full Command Summary

git init
gh auth login
gh repo create repository-name --public --source=. --remote=origin
git add .
git commit -m "Initial commit"
git branch -M main
git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Conclusion

Using gh repo create allows you to create a GitHub repository directly from the terminal, connect it as origin, and push your local project without manually creating the repository from the browser.

It is a faster, cleaner, and more professional workflow for starting new projects on GitHub.

Top comments (0)