What you can do after this lesson
You will understand how your computer proves its identity to GitHub, and be able to choose between the two usual methods, SSH keys and HTTPS with a token. This lesson is explained in words, with a few commands shown without output, because creating an account and uploading keys happens in a web browser and depends on your private details. Websites change their menus, so where we mention a setting, search GitHub's own documentation (docs.github.com) for the current steps. Nothing here was run against a real account.
What a GitHub account is for
Git needs no account. GitHub, the hosting service, does. An account gives you:
- A profile with a username that appears in URLs, such as
github.com/your-name/project. - The ability to create repositories, public (anyone can read) or private (only people you choose).
- Collaboration features: issues, pull requests, reviews, project boards.
- Permissions: who can read, who can push, who can administer a repository.
To sign up you choose a username, give an email address and a password, and verify the email. Pick a username you would be comfortable putting on a CV, since it is public and appears in every project URL. Other hosting services such as GitLab and Bitbucket work along similar lines, so the ideas carry over.
Protect the account
Before anything else, enable two-factor authentication in your account's security settings, and use a long, unique password kept in a password manager. If your account is taken over, so is every repository it can write to. Take a moment to also store a recovery method, so losing a phone does not lock you out.
The identity problem
When you run git push, GitHub has to be sure the request comes from someone allowed to write. There are two common ways.
HTTPS with a token or credential helper
The remote URL starts with https://, for example https://github.com/your-name/shelf.git. GitHub does not accept your account password for Git operations. Instead you use a personal access token (PAT): a long random string, created in your account's developer settings, that stands in for a password. When you create it you give it a name, an expiry date and a list of permissions, called scopes. A token limited to one repository with write access to contents is much safer than a broad one.
Most people never type the token repeatedly. They install Git Credential Manager (bundled with Git for Windows and available for macOS and Linux), or use the GitHub command line tool, either of which signs in through the browser and stores the credential in the operating system's secure store. Git's credential.helper setting tells it which helper to call. Check the current instructions in GitHub's documentation for your system.
SSH keys
The remote URL looks like git@github.com:your-name/shelf.git. Here you use a key pair: two linked files generated on your computer.
- The private key stays on your machine. It must never be shared, emailed, pasted or committed. A passphrase on it adds protection if the file is copied.
- The public key is safe to share. You add it to your GitHub account, and GitHub uses it to check signatures made by your private key.
Generating a pair is a local command. As documented by GitHub, a modern choice is the Ed25519 type:
ssh-keygen -t ed25519 -C "asha@example.com"It asks where to save the files and for an optional passphrase. The public file ends in .pub. You paste its contents into GitHub's SSH keys settings page. Then Git commands against git@github.com: addresses work without typing credentials, provided the SSH agent has your key loaded. The first connection asks you to confirm the server's fingerprint; compare it with the value GitHub publishes before accepting, because that check protects you from an impostor server.
Which should you pick?
| HTTPS + token or helper | SSH key | |
|---|---|---|
| Works through strict firewalls | Usually yes | Sometimes blocked |
| Setup | Sign in via a helper | Generate and register a key |
| Per-machine revocation | Revoke the token | Remove the key from the account |
| Typing credentials | Rare with a helper | None once the key is loaded |
Both are fine. Many developers use SSH on their own machines and tokens in automation. Use one consistently per remote so you are not confused by an unexpected prompt.
Setting the remote
To connect an existing local repository to a new, empty GitHub repository, you add a remote and push. These two commands talk to a real server, so they are shown without output:
git remote add origin git@github.com:your-name/shelf.git
git push -u origin maingit remote add itself only records the address; the push is where authentication happens. The same lines work with an HTTPS URL. To see which style a clone is using, run git remote -v.
Keeping credentials safe
- Never commit a private key, token or
.envfile. If you did, revoke it immediately: removing the file later does not remove it from history, as the.gitignorelesson explained. - Give tokens the fewest permissions and the shortest expiry that gets the job done.
- Do not paste tokens into chat, issue comments or screenshots.
- On a shared or borrowed computer, do not save credentials.
- Review the list of registered keys and tokens occasionally, and delete ones you no longer use.
GitHub also scans public repositories for some well-known secret formats and may warn the owner, but that is a last line of defence, not a plan.
Common mistakes
- Typing your account password where a token is expected. The push fails with an authentication error.
- Committing a key because it sat in the project folder. Keep keys in
~/.ssh, outside any repository. - Sharing the private key instead of the
.pubfile. If this happens, treat the key as compromised and replace it. - Using one broad token everywhere. A leak then exposes everything.
- Skipping two-factor authentication. It is the cheapest protection you have.
Exercise
- Write down, in your own words, the difference between a public and a private key.
- List which of these should never appear in a repository:
id_ed25519,id_ed25519.pub, a token, a README. - Decide whether you would use SSH or HTTPS on your main computer, and give one reason.
Show one possible answer
- The public key is a lock you can hand out; the private key is the only key that opens it and stays secret.
id_ed25519(private key) and the token must never appear. The.pubfile is safe to share, though there is rarely a reason to commit it, and a README is of course fine.- For example: "SSH, because once the key is loaded I never type credentials", or "HTTPS with a credential helper, because it works behind my office firewall".
Key points
- GitHub hosts Git repositories online; your account identifies you and controls who may read or write each repository.
- Your computer proves who it is either over HTTPS with a token (never your account password) or over SSH with a key pair.
- An SSH key pair has a private half that never leaves your machine and a public half you register on GitHub.
- A personal access token acts like a password with limited, named permissions that you can revoke.
- Turn on two-factor authentication and never commit keys or tokens to any repository.