Create a GitHub repository from a Linux environment sing the GitHub CLI

Lokesh JhaLokesh Jha
3 min read

Creating a GitHub repository from a Linux environment can be accomplished efficiently using the GitHub CLI (gh). Below is a step-by-step guide on how to install the GitHub CLI, log in, and create a new repository.

Step 1: Install GitHub CLI (gh)

To get started, you need to install the GitHub CLI on your Linux system. You can do this using the package manager specific to your Linux distribution.

For Debian/Ubuntu-based systems:

sudo apt update
sudo apt install gh

For Fedora:

sudo dnf install gh

For Arch Linux:

sudo pacman -S github-cli

Verify Installation

After installation, you can verify that the CLI is installed correctly by checking the version:

gh --version

Step 2: Log in to GitHub

Once the GitHub CLI is installed, you need to log in to your GitHub account. You can do this using either the HTTPS method or SSH.

Option A: Login via HTTPS

  1. Run the following command:

     gh auth login
    
  2. Choose GitHub.com as the platform.

  3. Select HTTPS as the preferred protocol.

  4. Follow the prompts to authenticate using your GitHub username and password or a personal access token.

Option B: Login via SSH

  1. If you prefer to use SSH, ensure you have an SSH key set up. If not, you can generate one using:

     ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    
  2. Add the SSH key to your GitHub account by copying the key:

     cat ~/.ssh/id_rsa.pub
    
  3. Run the login command:

     gh auth login
    
  4. Choose GitHub.com as the platform and select SSH as the preferred protocol.

  5. OR If you have already having a SSH key from your GitHub just paste in terminal when asked for.

     lokesh@r:~/Documents/workspace$ gh auth login
     ? What account do you want to log into? GitHub.com
     ? What is your preferred protocol for Git operations? SSH
     ? Generate a new SSH key to add to your GitHub account? No
     ? How would you like to authenticate GitHub CLI? Paste an authentication token
     Tip: you can generate a Personal Access Token here https://github.com/settings/tokens
     The minimum required scopes are 'repo', 'read:org'.
     ? Paste your authentication token: ****************************************
     - gh config set -h github.com git_protocol ssh
     ✓ Configured git protocol
     ✓ Logged in as lokesh1jha
    

Step 3: Create a New Repository

Now that you are logged in, you can create a new repository using the following command:

gh repo create <repository-name>

Additional Options

  • Set Visibility: You can specify whether the repository should be public or private by adding the --public or --private flag:

      gh repo create <repository-name> --private
    
  • Add a Description: To include a description for your repository, use the --description flag:

      gh repo create <repository-name> --description "This is my new repository"
    
  • Initialize with a README: To create the repository with an initial README file, add the --readme flag:

      gh repo create <repository-name> --readme
    

Example Command

Here’s a complete example command that creates a private repository named "my-awesome-repo" with a README:

gh repo create my-awesome-repo --private --readme --description "This is my awesome repository"

Conclusion

You have now successfully installed the GitHub CLI, logged into your GitHub account, and created a new repository directly from your Linux terminal. This streamlined process allows you to manage your GitHub repositories efficiently without needing to navigate through the web interface.

0
Subscribe to my newsletter

Read articles from Lokesh Jha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Lokesh Jha
Lokesh Jha

I am a senior software developer and technical writer who loves to learn new things. I recently started writing articles about what I've learned so that others in the community can gain the same knowledge. I primarily work with Node.js, TypeScript, and JavaScript, but I also have past experience with Java and C++. Currently, I'm learning Go and may explore Web3 in the future.