Create and Push a GitHub Repository from the Terminal
A step-by-step guide to installing and using the GitHub CLI.

As a software developer and technical writer, I specialise in creating informative articles on software development, computer programming, and various other technology-related subjects. I focus on providing clear and concise explanations, particularly tailored for beginners in the field. I make complex concepts accessible and enjoyable to learn for all readers.
In my dream of owning a housing estate, I will name it "The Terminal". It would encompass everything a city needs to survive and flourish and I will never have to leave it.
~ Patrick Okafor
Introduction
Context switching kills productivity. Every time you leave your terminal to click around a browser, you break your flow state.
This guide shows you how to install the GitHub CLI (Command Line Interface), create a new repository, add files, and push your code to GitHub, all from your terminal. We will use Git and the GitHub CLI (gh), a tool that brings GitHub's features to your command line.
By the end, you will not only know the manual commands but also have a custom automation script to handle the entire process in seconds.
Prerequisites
This guide is for developers who want to learn the command-line workflow for managing GitHub projects.
Before you begin, you must have:
Comfort using a terminal.
A GitHub account.
Git is installed on your system.
Step 1: Install and Authenticate the GitHub CLI
First, we install gh, the tool that brings GitHub's features to your command line.
Install the CLI
Run the following script (for Debian/Ubuntu Linux) to install
gh:# Install and authenticate gh using the official script for Debian/Ubuntu-based distributions. (type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y)) \ && sudo mkdir -p -m 755 /etc/apt/keyrings \ && out=$(mktemp) && wget -nv -O$out [https://cli.github.com/packages/githubcli-archive-keyring.gpg](https://cli.github.com/packages/githubcli-archive-keyring.gpg) \ && cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \ && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \ && sudo mkdir -p -m 755 /etc/apt/sources.list.d \ && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] [https://cli.github.com/packages](https://cli.github.com/packages) stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \ && sudo apt update \ && sudo apt install gh -yNote: For macOS (
brew install gh) or Windows (winget install GitHub.cli), refer to the official installation guide.Authenticate
Once installed, open your terminal and run the following command to log in to GitHub:
gh auth loginThis command authenticates gh and git with GitHub, starting an interactive setup:
❓ Where do you use GitHub?: Choose
GitHub.comas your location.❓ What is your preferred protocol for Git operations on this host?: Choose HTTPS or SSH. I prefer the SSH protocol for working with GitHub because it is more secure and avoids the sign-in bottlenecks I sometimes encounter with HTTPS. To switch from HTTPS to SSH with GitHub, read this documentation.
❓ Upload your SSH public key to your GitHub account?: Select "Yes" to upload your SSH public key to your GitHub account at the default location. If you saved your public key in a different location, use that instead.
❓ Title for your SSH key: Choose any name you like for your SSH key title. It's best to use names that are easy to recognise and related to their purpose.
❓ How would you like to authenticate GitHub CLI?: Log in and authenticate the GitHub CLI using a web browser, preferably the one you use for GitHub.

Copy the one-time code displayed in your terminal. Press Enter, and paste the code into the browser window that opens.
Note: If, after you press Enter and you encounter an error, don't worry. Just copy the URL manually, open it in your web browser, and sign in.

After you have signed in, you will be directed to a new page where you have to enter the one-time code provided to you in the terminal during authentication. Never use a code sent by someone else.

Congratulations, your account has been authenticated and authorised. You’re set.

Verify the installation
To confirm that the CLI is installed and authenticated, run:
gh --version # Output: gh version 2.x.x (2024-xx-xx)This command should display the installed version of the
ghCLI.
Step 2: Create and Initialise Your Local Project
Next, create a directory for your new project and initialise it as a Git repository. Let's set up a mock project to test our workflow.
# 1. Create and enter the directory
mkdir E-commerce
cd E-commerce
# 2. Initialize Git
git init
# 3. Create a README and commit it
echo "# E-commerce Project" > README.md
git add README.md
git commit -m "Initial commit"
You now have a local Git repository with one commit.
Step 3: Create and Push to GitHub(The "Power User" Way)
In a traditional workflow, you would go to GitHub.com, create a repo, copy the link, add the remote, and push. With gh, we can do this in one command. This is where the GitHub CLI saves time.
Run the gh repo create command from inside your E-commerce directory:
gh repo create --public --source=. --remote=origin --push
Breaking down the flags:
--public: Creates a public repository (use--privatefor private work).--source=.: Tells GitHub to use the current directory as the source code.--remote=origin: Automatically adds the remote git URL to your local config.--push: Pushes your current commits immediately.
Step 4: Link and Push Your Local Repository
Finally, connect your local repository to the new remote repository on GitHub and push your code.
Add the remote URL
Git needs to know the URL of the remote repository. Use the
git remote addcommand to link your local repository to the GitHub URL.Note: Replace
patoski-patoskiwith your actual GitHub username.git remote add origin https://github.com/patoski-patoski/E-commerce.gitoriginis the default shorthand name for the remote repository.
2. Push your local branch
Upload your local
main(ormaster) branch to theoriginremote repository.git push -u origin maingit pushuploads your commits.-u origin mainsets the localmainbranch to track themainbranch on theoriginremote. This allows you to use a simplegit pushcommand for future updates.
Success! You have just created and pushed a repository without opening a browser.
Bonus: Automate It with a Bash Function
Typing those flags every time is tedious. As engineers, we should automate repetitive tasks.
Add this function to your .bashrc or .zshrc file to turn this 4-step process into a single command.
# Add this to your shell configuration file
create_repo() {
# 1. Create directory and enter it
mkdir "$1"
cd "$1" || return
# 2. Initialize Git and create a dummy file
git init
echo "# $1" > README.md
git add README.md
git commit -m "Initial commit"
# 3. Use Github CLI to create and push in one go
# Usage: create_repo <repo_name>
gh repo create "$1" --public --source=. --remote=origin --push
echo "🚀 Repository '$1' live on GitHub!"
}
How to use it: Now, whenever you start a project, simply run:
create_repo my-new-project
Summary
You have successfully moved your workflow from the browser to the terminal. You learned how to:
Authenticate securely with
gh.Use flags to combine creation and pushing into one step.
Write a shell script to automate the entire lifecycle.
NEXT STEPS




