“I forgot to create the GitHub repo again.”

“I forgot to create the GitHub repo again.”
Photo by Yancy Min / Unsplash

One command that fixes it

If you use Git a lot, you’ve lived this scene:

You’re in a new project folder. You do the disciplined thing:

  • git init
  • add files git add --all
  • commit git commit -m "commit message"

And then the satisfying final move:

git push

…and Git answers:

“Cool story. That repo doesn’t exist.”

So you sigh, open a browser, click around GitHub’s UI, create the repository, copy the remote URL, come back to the terminal, add the remote, and push again.

It’s not hard. It’s just unreasonably manual for people who type for a living.

The funny part: this has been solved for a long time. I just didn’t install the tool.

The fix: GitHub CLI (gh)

GitHub’s CLI can create the remote repository and push to it in one go. The “missing repo” step disappears.


Install gh

macOS (Homebrew)

brew install gh

Linux

On many distros, the package is already available.

Debian/Ubuntu:

sudo apt update
sudo apt install gh

Fedora:

sudo dnf install gh

If your distro package is old, GitHub provides official instructions to add their repo (search “GitHub CLI install linux” and follow the official doc for your distro).

Windows (native)

If you use Windows Terminal / PowerShell:

Winget (recommended):

winget install -e --id GitHub.cli

Verify anywhere:

gh --version

Windows (WSL2)

If you use WSL2 Ubuntu, you’re basically a Linux user:

sudo apt update
sudo apt install gh

Authenticate once

gh auth login

What it typically asks:

  • GitHub.com or GitHub Enterprise?Most people choose GitHub.com.
  • Preferred protocol for Git operations?I prefer SSH. (HTTPS is fine too, but SSH is “set it once and forget it” on dev machines.)
  • How do you want to authenticate?Usually either:
    • Login with a browser (fastest, least pain)
    • Paste a token (works everywhere; you create a PAT on GitHub and paste it)

After that, gh is ready.


The workflow: create repo + push in one command

Let’s say you have a fresh project:

git init
git add --all
git commit -m "initial commit"

Now the punchline:

gh repo create ap --public --source=. --remote=origin --push

That does all of this at once:

  • creates a GitHub repo named ap
  • sets origin to the right URL
  • pushes your current branch
  • sets upstream tracking

Private repo:

gh repo create ap --private --source=. --remote=origin --push

No browser. No clicking. No copy-pasting remote URLs.


Make it even shorter: gcp reponame

Yes — you can automate this into a shell function.

macOS + Linux + WSL2 (bash/zsh): add to ~/.zshrc or  ~/.bashrc

gcp () {
  local vis="--public"                   # change to "--private" if you want
  local name=""

  # flags: -p private, -P public
  while [ $# -gt 0 ]; do
    case "$1" in
      -p) vis="--private"; shift ;;
      -P) vis="--public"; shift ;;
      --private|--public) vis="$1"; shift ;;
      *) name="$1"; shift; break ;;
    esac
  done

  # ignore any extra args after repo name
  # (keeps it simple and clojure-like)

  # determine repo name
  if [ -z "$name" ] || [ "$name" = "." ]; then
    name="$(basename "$PWD")"
  fi

  # init only if needed
  if [ ! -d .git ]; then
    git init >/dev/null
  fi

  # first commit if needed
  if ! git rev-parse --verify HEAD >/dev/null 2>&1; then
    git add -A
    git commit -m "initial commit"
  fi

  # create repo and push
  gh repo create "$name" "$vis" --source=. --remote=origin --push
}

Reload:

source ~/.zshrc   # or ~/.bashrc

Now:

gcp            # uses current folder name, public
gcp .          # same
gcp -p         # uses current folder name, private
gcp -P         # uses current folder name, public
gcp myrepo     # explicit name, public
gcp -p myrepo  # explicit name, private

Windows (PowerShell): add a function to your profile

In PowerShell:

notepad $PROFILE

Add:

function gcp {
  param(
    [Parameter(Position=0)][string]$name = ".",
    [switch]$p,
    [switch]$P
  )

  $vis = "--public"                # change to "--public" if you want
  if ($p) { $vis = "--private" }   
  if ($P) { $vis = "--public" }

  if ([string]::IsNullOrWhiteSpace($name) -or $name -eq ".") {
    $name = Split-Path -Leaf (Get-Location)
  }

  if (-not (Test-Path ".git")) { git init | Out-Null }

  git rev-parse --verify HEAD *> $null
  if ($LASTEXITCODE -ne 0) {
    git add -A
    git commit -m "initial commit"
  }

  gh repo create $name $vis --source=. --remote=origin --push
}

Reload the profile (or reopen the terminal):

. $PROFILE

Now:

gcp           # current folder name, public
gcp .         # same
gcp -p        # current folder name, private
gcp -P        # current folder name, public
gcp myrepo    # explicit name, public
gcp myrepo -p # explicit name, private

Why this feels so good

It removes one of those tiny frictions that interrupts flow. The “open browser, click repo name, copy URL” dance isn’t difficult — it’s just not what you want to be doing while coding.

Once you have gh, “create remote + push” becomes as natural as git commit.

And yes: it’s slightly embarrassing how long many of us went without this.


Appendix: Generate an SSH key

To use SSH with GitHub you don’t create an “SSH token” — you create an SSH key pair, then add the public key to GitHub. Here’s the straight path.

macOS / Linux / WSL2

1) Generate a key

Recommended modern key type:

ssh-keygen -t ed25519 -C "your_email@example.com"

When it asks where to save it, press Enter for the default:

  • ~/.ssh/id_ed25519 (private key)
  • ~/.ssh/id_ed25519.pub (public key)

It will also ask for a passphrase (recommended).

If you’re on an older system that doesn’t support ed25519:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

2) Start ssh-agent and add the key

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

3) Copy your public key

cat ~/.ssh/id_ed25519.pub

4) Add it to GitHub

GitHub → SettingsSSH and GPG keysNew SSH key → paste the public key.

(That’s the only part you must do in the browser.)

5) Test

ssh -T git@github.com

You should see a message like “You’ve successfully authenticated…”


Windows (native)

Option A: Windows Terminal / PowerShell (OpenSSH)

Generate:

ssh-keygen -t ed25519 -C "your_email@example.com"

Start agent + add key:

Get-Service ssh-agent | Set-Service -StartupType Automatic
Start-Service ssh-agent
ssh-add $env:USERPROFILE\.ssh\id_ed25519

Show public key:

type $env:USERPROFILE\.ssh\id_ed25519.pub

Add it on GitHub (same place as above), then test:

ssh -T git@github.com

Option B: WSL2

Just follow the Linux steps inside Ubuntu.


Make Git use SSH remote

If your repo remote is HTTPS and you want SSH:

git remote set-url origin git@github.com:YOURUSER/YOURREPO.git

For a new repo with gh, you can also choose SSH during:

gh auth login