dev/notes

Spot a mistake? Highlight any text in a post and click Report — it goes straight to the author.

beginner · Git · August 17, 2026 · 2 min read

How to Set Up Git and GitHub from the Command Line

Set up Git and GitHub from the command line: SSH keys, your first commit, branches, and pull requests. Everything you need to start version-controlling projects.

Why SSH keys matter

You can push to GitHub with a username and password, but SSH keys are both more convenient and more secure. Generate one key pair once, register the public key with GitHub, and every repo you clone authenticates without prompts.

Step 1 — Generate an SSH key

ssh-keygen -t ed25519 -C "[email protected]"

Press Enter to accept the default location, and set a passphrase (recommended). Then copy the public key — never share the private key:

# macOS
pbcopy < ~/.ssh/id_ed25519.pub
# Linux
xclip -sel clip < ~/.ssh/id_ed25519.pub
# Windows (PowerShell)
Get-Content ~/.ssh/id_ed25519.pub | Set-Clipboard

Step 2 — Add the key to GitHub

  1. Go to GitHub → Settings → SSH and GPG keys → New SSH key.
  2. Paste the public key and save.

Verify the connection:

ssh -T [email protected]
# Hi username! You've successfully authenticated.

Step 3 — Push your first repository

cd my-project
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin [email protected]:you/my-project.git
git push -u origin main

Step 4 — Branches and pull requests

Feature branches keep main shippable. Instead of committing directly:

git checkout -b feature/add-readme
# edit files...
git add .
git commit -m "Add project readme"
git push -u origin feature/add-readme

Open the URL Git prints, and create a pull request. Review, merge, then tidy up:

git checkout main
git pull
git branch -d feature/add-readme

Step 5 — Add a .gitignore

Keep build output and secrets out of history:

node_modules/
dist/
.env
.env.*
!.env.example

Commit the .gitignore before you commit anything else, so those files never enter history — once committed, removing a secret requires rewriting the repo.

Troubleshooting

  • Permission denied (publickey) — the key isn’t registered, or ssh-agent doesn’t have it. Try ssh-add ~/.ssh/id_ed25519 and re-test.
  • remote origin already existsgit remote set-url origin [email protected]:you/repo.git.
  • A secret got committed — treat it as compromised, rotate it immediately, and use a tool like git filter-repo to scrub history.

Summary

You now have passwordless GitHub auth over SSH, a clean first-push workflow, and a branch-and-PR habit that keeps main safe. Next: automate that workflow with GitHub Actions.

Related posts

Comments

One comment per thread every 30 minutes · edits are unlimited.