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
- Go to GitHub → Settings → SSH and GPG keys → New SSH key.
- Paste the public key and save.
Verify the connection:
ssh -T [email protected]
# Hi username! You've successfully authenticated.
If you set a passphrase (you should), ssh-agent saves you from typing it on
every push:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
On macOS, ssh-add --apple-use-keychain stores the passphrase in Keychain so
it survives reboots. If you have several keys (one for work, one for personal),
give each a distinct filename — ssh-keygen -t ed25519 -f ~/.ssh/id_work — and
register the right one per host with an ~/.ssh/config entry:
Host github.com
IdentityFile ~/.ssh/id_work
Prefer HTTPS instead? git clone https://github.com/you/repo.git works with
your GitHub password or a personal access token as the password, and GitHub
already requires tokens for password auth. SSH is still the better default — no
token to leak in URLs or logs.
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.
Reading and understanding Git’s output
Most Git failures are confusing only because the messages are terse. git status
shows what’s staged versus modified; git log --oneline --graph --all shows the
whole branch picture; and when a command rejects you, git config --list tells
you what identity and remotes Git is actually using. Checking those three
things answers most “why won’t this work” moments before you search anything.
Troubleshooting
Permission denied (publickey)— the key isn’t registered, orssh-agentdoesn’t have it. Tryssh-add ~/.ssh/id_ed25519and re-test.remote origin already exists—git 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-repoto 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 for CI/CD, or continue with the modern JavaScript development environment guide.
Comments
One comment per thread every 30 minutes · edits are unlimited.