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
brew install gh
On Windows
scoop install gh
On Linux
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:
gh auth login
2. Creating a New Repository
You can create a new repository by using:
gh repo create my-new-repo --public
Output:
✓ 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:
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
- Time-Saving: No need to manually create directories or initialize repositories.
- Consistency: Ensures that every project follows the same setup and structure.
- Ease of Use: A simple command executes multiple setup steps.
- 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.
#! /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
- Save the script in a file with the
.shextension, such ascreate-project-with-git.sh. - Open the terminal.
- Navigate to the directory where you want to create the new project.
- 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. - Follow the prompts in the terminal.
Example
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.
# 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
- Save the script in a file with the
.ps1extension, such ascreate-project-with-git.ps1. - Open PowerShell.
- Navigate to the directory where you want to create the new project.
- 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. - Follow the prompts in PowerShell.
Example
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
Is Github Cli available on all platforms? Yes, Github Cli is available on macOS, Windows, and Linux.
Can I create private repositories using Github Cli? Absolutely. Replace
--publicwith--privatein the repository creation command.How do I clone a repository using Github Cli? Use the command
gh repo clone <repository>to clone a repository.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.
Is Github Cli free to use? Github Cli is an open-source tool and is free to use.