Using GitHub CLI to Create a Private Repository and Upload Code

19 min read

!github cli

The GitHub CLI (gh) is a powerful tool that allows developers to interact with GitHub repositories directly from the command line. This tutorial provides a quick guide on how to use gh to create a private repository and push your local code to GitHub.

1. Install GitHub CLI

Before proceeding, make sure that you have the GitHub CLI installed and configured. You can install gh using the following commands:

macOS (via Homebrew):

brew install gh

Ubuntu (via APT):

sudo apt install gh

Windows (via Scoop):

scoop install gh

Once installed, log in to your GitHub account with:

gh auth login

2. Create a Private Repository and Push Your Code

If you already have a Git project initialized locally with code ready to be uploaded, gh makes it easy to create a private repository and push your code with a single command.

In the root of your project directory, run the following:

gh repo create <your-repo-name> --private --source=. --remote=origin --push

Explanation:

  • <your-repo-name>: This is the name of the repository you want to create on GitHub.
  • --private: Specifies that the repository will be private, meaning only you and authorized users can access it.
  • --source=.: Tells gh to use the current directory as the source for the repository.
  • --remote=origin: Automatically sets the remote repository name to origin.
  • --push: Pushes your local code to the newly created GitHub repository immediately after creation.

3. Successfully Creating and Pushing Code

After running the command, you'll see an output with the repository URL, indicating that your code has been successfully pushed to the private GitHub repository. You can now access the repository through a web browser or continue working with git push to update your code from the command line.

4. Additional Useful gh Commands

Here are some other commonly used gh commands:

  • List all repositories:

    gh repo list
    
  • Clone a repository:

    gh repo clone <repo-name>
    
  • Open the repository in the browser:

    gh repo view --web
    

Conclusion

The gh CLI tool offers developers a fast and easy way to manage GitHub repositories. With the steps outlined above, you can quickly create a private repository and push your local code to GitHub. The gh tool enhances productivity by reducing the need to switch between applications and provides efficient repository management directly from the command line.