Working with GitHub from Linux CLI

This guide shows how to create and manage a GitHub repository entirely from the Linux command line.


1. Setup Git

sudo apt update
sudo apt install git

Configure your identity (used for commits):

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Check configuration:

git config --list

ssh-keygen -t ed25519 -C "your.email@example.com"

Copy the public key:

cat ~/.ssh/id_ed25519.pub

Add it to GitHub under Settings → SSH and GPG keys → New SSH key.

Test connection:

ssh -T git@github.com

3. Create a Local Repository

mkdir my-project
cd my-project
git init

This creates a new Git repository in the folder.

Add a file and commit it:

echo "# My Project" > README.md
git add README.md
git commit -m "Initial commit"

4. Create a GitHub Repository from CLI

Using HTTPS (if SSH not configured)

Go to https://github.com/new and create an empty repository manually.

Then connect it:

git remote add origin https://github.com/username/my-project.git

Using SSH (preferred)

git remote add origin git@github.com:username/my-project.git

Push the local repo to GitHub:

git branch -M main
git push -u origin main

5. Making Changes and Updating the Repository

Edit files as needed, then:

git status
git add .
git commit -m "Describe changes"
git push

To pull updates from GitHub:

git pull

6. Cloning Existing Repositories

git clone https://github.com/username/repository.git
# or
git clone git@github.com:username/repository.git

7. Branching

Create and switch to a new branch:

git checkout -b feature-branch

Push a new branch to GitHub:

git push -u origin feature-branch

Merge back to main:

git checkout main
git merge feature-branch
git push

Summary


End of Guide