GitHub Basics: Repos, SSH & Tokens

Git · Lesson 16 of 28 8 min read Updated

GitHub is the most popular home for Git repositories. It stores a copy of your project online, shows your code and history in the browser, and adds team features such as pull requests, issues and GitHub Actions, which the next lessons cover. For students it doubles as a portfolio: recruiters often look at a candidate's GitHub profile.

In this lesson you will create a repository on GitHub, connect Priya's local project to it, set up a secure login with SSH keys or a personal access token, and write a README that explains the project.

Your account

Sign up at github.com with your email and choose a username carefully: it appears in every repository URL, such as github.com/priya-sharma, so pick something professional. Then do two things straight away in Settings:

  • Turn on two-factor authentication (2FA) with an authenticator app. GitHub requires 2FA for accounts that contribute code, and it protects your account if your password leaks.
  • Under Emails, check the address Git uses (user.email) is listed, so your commits link to your profile. You can also choose to keep your address private and use the noreply address GitHub shows there.

Creating a repository

Click the + menu at the top right and choose New repository. You fill in:

FieldWhat to choose
Owner and nameYour account and a short name, such as sweet-shop
DescriptionOne line about the project (optional)
Public or PrivatePublic: anyone can see it. Private: only you and people you invite
Add READMETick it for a brand new project; leave it unticked if you will push an existing one
.gitignore and licenseOptional templates; skip them when pushing an existing project

Priya already has a local repository with history, so she creates an empty repository (no README, no .gitignore, no license). If GitHub's copy already contained a commit, her first push would be rejected because the histories would not match.

GitHub then shows the commands to connect an existing repository. They are the commands from the previous lesson:

Terminal
git remote add origin git@github.com:priya-sharma/sweet-shop.git
git branch -M main
git push -u origin main

git branch -M main renames the current branch to main, just in case it is still called master. Before the push can work, though, GitHub needs to know it is really Priya.

Logging in: HTTPS or SSH

GitHub stopped accepting account passwords for Git commands in August 2021. You have two secure choices.

HTTPS with Git Credential Manager (easiest on Windows). Git for Windows includes Git Credential Manager. The first time you push to an https://github.com/... address, a browser window opens, you sign in to GitHub, and the login is stored safely for next time. macOS and Linux users can install Git Credential Manager too.

HTTPS with a personal access token. A personal access token (PAT) is a long random password that you create for a specific purpose and can revoke at any time. Create one in Settings > Developer settings > Personal access tokens. GitHub offers two kinds:

  • Fine-grained tokens: limited to chosen repositories with specific permissions (for example, "Contents: read and write" on sweet-shop only). GitHub recommends these.
  • Tokens (classic): broad scopes such as repo that apply to every repository you can access.

Give every token an expiry date and the smallest permissions that work. When Git asks for a password over HTTPS, paste the token instead. Treat it exactly like a password: never commit it, never paste it into chat.

SSH keys. SSH uses a pair of keys: a private key that never leaves your computer and a public key you give to GitHub. Generate a pair (press Enter to accept the default file; a passphrase is recommended):

Terminal
ssh-keygen -t ed25519 -C "priya@example.com"
Output
Generating public/private ed25519 key pair.
Your identification has been saved in /c/Users/priya/.ssh/id_ed25519
Your public key has been saved in /c/Users/priya/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:4eTgiZUtq8jjSt0aJyhATPXjiYSoPXQOVl1Vbow8Xq0 priya@example.com

(The questions about the file name and passphrase, and the key's "randomart" picture, are left out above.) Print the public key (the file ending in .pub) and copy the whole line:

Terminal
cat ~/.ssh/id_ed25519.pub
Output
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBN+5aoP1BJ3VtVpuIdlHNR6i4KWXRn+YFxCIPc0Fhh3 priya@example.com

On GitHub go to Settings > SSH and GPG keys > New SSH key, give it a title such as "Priya's laptop", paste the line and save. Then test the connection:

Terminal
ssh -T git@github.com

The first time, SSH shows GitHub's key fingerprint and asks whether to continue; compare it with the fingerprints GitHub publishes in its documentation, then type yes. If the key is set up correctly, you see a greeting with your username: Hi priya-sharma! You've successfully authenticated, but GitHub does not provide shell access. If the key is missing or wrong, you see this instead:

Output
git@github.com: Permission denied (publickey).

With SSH working, use git@github.com:... addresses. To switch an existing remote from HTTPS to SSH, run git remote set-url origin git@github.com:priya-sharma/sweet-shop.git.

The README

A README is the front page of a repository: GitHub displays README.md under the file list. It is written in Markdown, a simple text format where # makes a heading, - makes a list and backticks mark code. A good README answers: what is this, how do I run it, and how do I contribute? Here is Priya's:

Text
# Sharma Sweets website

Website for Sharma Sweets, a family sweet shop in Johari Bazaar, Jaipur.
It shows the menu, prices, opening hours and Diwali offers.

## Run it locally

1. Clone the repository.
2. Open `index.html` in any browser. No build step is needed.

## Project structure

- `index.html`: home page
- `menu.html`: menu and prices (update prices here)
- `main.css`: all styles

## Contributing

Open an issue first for big changes. Small fixes are welcome as pull requests.

GitHub renders the # lines as headings and the numbered lines as a list. Commit it like any file:

Terminal
git add README.md
git commit -m "Add README with setup steps"
git push

A profile README is a nice touch too: create a public repository with the same name as your username, and its README appears at the top of your GitHub profile.

Exploring a repository page

On any repository page you will find: Code (files, branches, tags and the green Code button with clone addresses), Issues, Pull requests, Actions, and Settings for owners. The commit count opens the history, every file has a History and Blame view, and pressing . on a repository opens it in a browser-based editor.

Common mistakes

  • Typing your GitHub password when Git asks for one. It will fail. Use Git Credential Manager's browser login, a token, or SSH.
  • Creating the GitHub repository with a README, then pushing an existing project. The push is rejected because the histories differ. Create it empty, or pull with --allow-unrelated-histories once.
  • Uploading the private SSH key or a token. Revoke it immediately if it ever lands in a commit or chat.
  • A README that only says the project name. Explain what it does and how to run it; that is what visitors and recruiters read.

Key points

  • GitHub hosts Git repositories and adds collaboration features; enable 2FA on your account.
  • Create an empty repository to push an existing project, then git remote add origin and git push -u origin main.
  • Account passwords do not work for Git; use Git Credential Manager, a fine-grained personal access token, or SSH keys.
  • Generate an SSH key with ssh-keygen -t ed25519, add the .pub file to GitHub, and test with ssh -T git@github.com.
  • README.md is your project's front page, written in Markdown.

Exercise

Create a free GitHub account if you do not have one. Set up an SSH key and test it. Create an empty public repository called git-practice, push any small local repository to it, then add a README with a title, a one-line description and a "How to run" section, and push again.

Show solution
Terminal
ssh-keygen -t ed25519 -C "you@example.com"
cat ~/.ssh/id_ed25519.pub
ssh -T git@github.com
git remote add origin git@github.com:your-username/git-practice.git
git branch -M main
git push -u origin main
git add README.md
git commit -m "Add README"
git push

Paste the .pub line into Settings > SSH and GPG keys before running ssh -T. After the last push, refresh the repository page and GitHub shows your formatted README below the file list.

Quick Quiz

1. Which file from an SSH key pair do you add to GitHub?

2. Why should you create the GitHub repository empty when pushing an existing local project?

3. What does GitHub recommend for HTTPS access when you need a token?

4. What does ssh -T git@github.com do?

Learn Git with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call