[Github Cli] Automating Repository Creation: Set Up Projects Without Accessing the Website

Learn how to create Github repositories using Github Cli without ever accessing the Github website. Follow this guide to streamline your workflow and enhance your project management skills.

Installation of Github Cli

Github Cli is a command-line tool that brings Github to your terminal. Here's how you can install it:

On macOS

bash
brew install gh

On Windows

bash
scoop install gh

On Linux

bash
sudo apt install gh

These commands will install Github Cli on your system.

Creating a Repository

With Github Cli, creating a new repository is a breeze. Follow these steps:

1. Authentication

First, authenticate your Github account by running:

bash
gh auth login

2. Creating a New Repository

You can create a new repository by using:

bash
gh repo create my-new-repo --public

Output:

bash
✓ Created repository user/my-new-repo on GitHub

This command creates a public repository named 'my-new-repo' under your account.

3. Pushing Your Code

To push your code to the newly created repository, use:

bash
git push origin main

This command pushes the code to the 'main' branch.

Automating Project Creation with Github CLI

Setting up a new project often involves several repetitive tasks such as creating a new directory, initializing a Git repository, and linking to Github. To simplify these tasks, we have developed automated scripts for macOS, Linux, and Windows. These scripts combine several steps into one, providing a convenient way to quickly initiate new projects.

Benefits of Using the Scripts

  1. Time-Saving: No need to manually create directories or initialize repositories.
  2. Consistency: Ensures that every project follows the same setup and structure.
  3. Ease of Use: A simple command executes multiple setup steps.
  4. Cross-Platform: Separate scripts for macOS, Linux, and Windows to cater to different environments.

Automated Script for macOS and Linux

For macOS and Linux users, the following bash script streamlines the process of creating a project directory, initializing a local Git repository, creating a corresponding private Github repository, and connecting them.

bash
#! /bin/bash

echo 'Create a project in the current folder and connect it to Github.'
echo 'Please enter the project name: ("Enter" to cancel)'
read PROJECT_NAME

if [ ! -n "$PROJECT_NAME" ]; then
  exit 1
fi

LOCAL_PATH=$(pwd)/$PROJECT_NAME

echo $LOCAL_PATH "Creating a project at this path. Type 'yes' if correct, any other key to cancel"
read CREATE

if [ -n "$CREATE" ]; then
  if [ $CREATE == yes ]
  then
    # Create a private Github repository
    gh repo create $PROJECT_NAME --private

    # Create local project
    mkdir "$LOCAL_PATH"
    cd "$LOCAL_PATH"
    echo "# "$PROJECT_NAME >> README.md

    # Initialize Git and connect to Github
    git init
    git remote add github [email protected]:USERNAME/$PROJECT_NAME
    git add .
    git commit -m "initial commit"
    git push github master
  fi
fi

Running the Script

  1. Save the script in a file with the .sh extension, such as create-project-with-git.sh.
  2. Open the terminal.
  3. Navigate to the directory where you want to create the new project.
  4. Navigate to the location where you want the project folder to be created, then execute the script using its full path. For example, run it with sh /path/to/your/script/create-project-with-git.sh.
  5. Follow the prompts in the terminal.

Example

bash
cd /path/to/your/project/directory
sh /path/to/your/script/create-project-with-git.sh

Automated Script for Windows

Windows users can utilize the PowerShell script to achieve the same functionality. This script takes care of creating the project locally and connecting it with Github.

powershell
# Prompt for project name
Write-Host 'Create a project in the current folder and connect it to Github.'
Write-Host 'Please enter the project name: ("Enter" to cancel)'
$PROJECT_NAME = Read-Host

if ([string]::IsNullOrWhiteSpace($PROJECT_NAME)) {
    exit
}

$LOCAL_PATH = (Get-Location).Path + "/" + $PROJECT_NAME

Write-Host $LOCAL_PATH "Creating a project at this path. Type 'yes' if correct, any other key to cancel"
$CREATE = Read-Host

if ($CREATE -eq 'yes') {
    # Create a private Github repository
    gh repo create $PROJECT_NAME --private

    # Create local project
    mkdir $LOCAL_PATH
    Set-Location $LOCAL_PATH
    echo "# "$PROJECT_NAME > README.md

    # Initialize Git and connect to Github
    git init
    git remote add github [email protected]:USERNAME/$PROJECT_NAME
    git add .
    git commit -m "initial commit"
    git push github master
}

Running the Script

  1. Save the script in a file with the .ps1 extension, such as create-project-with-git.ps1.
  2. Open PowerShell.
  3. Navigate to the directory where you want to create the new project.
  4. Navigate to the location where you want the project folder to be created, then execute the script using its full path.For example, run it in PowerShell with C:\path\to\your\script\create-project-with-git.ps1.
  5. Follow the prompts in PowerShell.

Example

powershell
cd "C:\path\to\your\project\directory"
C:\path\to\your\script\create-project-with-git.ps1

These automated scripts are more than just a convenience; they are essential tools for developers who wish to optimize their workflow and maintain uniformity across different projects. By integrating these scripts into your development process, you can focus more on coding and less on the repetitive setup tasks, thereby enhancing your overall productivity.


In conclusion, Github Cli simplifies the process of creating and managing repositories without the need to access the Github website. It helps to streamline the development workflow and offers a great way to enhance project management skills.


FAQs

  1. Is Github Cli available on all platforms? Yes, Github Cli is available on macOS, Windows, and Linux.
  2. Can I create private repositories using Github Cli? Absolutely. Replace --public with --private in the repository creation command.
  3. How do I clone a repository using Github Cli? Use the command gh repo clone <repository> to clone a repository.
  4. Can I manage issues and pull requests with Github Cli? Yes, Github Cli allows you to manage issues and pull requests from the command line.
  5. Is Github Cli free to use? Github Cli is an open-source tool and is free to use.
© Copyright 2023 CLONE CODING