logo
cd ..

How to Create a GitHub Repository from your Terminal

Comments
1 min

How to Create a GitHub Repository from your Terminal

Learn how to create and manage GitHub repositories directly from your terminal using the GitHub CLI tool

How to create a github repository using the command line cover image

Managing GitHub repositories from your terminal can significantly enhance your development workflow. By using the command line, you can quickly create and initialize repositories, manage commits, and push changes without relying on a graphical interface. This guide will take you step-by-step through the process, making it easy to harness the power of Git and GitHub directly from your terminal.

Install GitHub CLI

GitHub CLI , or gh, is an open-source command-line interface to GitHub for use in your terminal or your scripts. You need to install it into your PC to start using it. Visit the link above to download and install it into your machine.

Create a New Repository

Open up a terminal and run the below command to create a new repository

terminal

gh repo create

This will prompt an interactive window you can use to configure your repository details.

terminal

What would you like to do? Create a new repository on GitHub from scratch
? Repository name test

? Repository name test
? Repository owner Evavic44
? Description Testing GitHub CLI

? Description Testing GitHub CLI
? Visibility Public
? Would you like to add a README file? Yes
? Would you like to add a .gitignore? Yes
? Would you like to add a license? Yes
? This will create "test" as a public repository on GitHub. Continue? Yes
āœ“ Created repository Evavic44/test on GitHub
  https://github.com/Evavic44/test-cli
? Clone the new repository locally? No

Once completed, you should have your repository successfully created on GitHub. Now you can point your local project to the remote branch and push it to GitHub.

terminal

git remote add origin https://github.com/Evavic44/test.git
git add .
git commit -m 'Initial commit'
git push origin main

If you choose to create a README or License file during the gh repo create set-up, trying to pull it into a local project will obviously throw an error due to unrelated histories between the remote and local branch.

git unrelated histories issue
git unrelated histories issue

To resolve this, you can force the merging using the below command:

terminal

git pull origin main --allow-unrelated-histories

If you get any conflicts, simply fix it, commit and then push to GitHub.

Aside form creating a new repository, you can perform other operations using the GitHub CLI tool. Here's a simple list showing a few popular commands.

Common GitHub CLI Commands
CommandsDescription
gh repo listLists your repositories
gh issue createCreate an issue on GitHub
gh issue listLists all issues in current project
gh gist list View your created gists
gh gist create [filename.extension]Publishes file as a new gist on GitHub
gh pr createCreate a new pull request if current branch has new changes
gh pr listLists all open pull request in project
gh project listView your open projects (requires authentication)
gh release createCreate a new release for project

To see more, visit the GitHub CLI Commands documentation.

Comments

Support

Do you feel like supporting my work? šŸ™‚Buymeacoffee button