Your repository on GitHub · lesson 8 of 9
Commands and ideas in this lesson
AGit Credential Managersigns in through your browser, then remembers
BSSH keya public half on GitHub, a private half at home
Cgit remote add originconnect to the GitHub copy
Dgit push -u origin mainsend it up the first time

Signing in and pushing to GitHub

Last updated

Ana's Kusina, with all its history from Stages 1 to 3, is still only on her laptop. In this lesson you'll set up Git on your computer to sign in to GitHub safely, connect an existing repository to an empty one on GitHub, and push it up.

By the end of this lesson you can

  • Explain why your GitHub password doesn't work in Git, and what to use instead
  • Set up sign-in with Git Credential Manager, the GitHub CLI or an SSH key, by following GitHub's guide
  • Connect an existing repository to an empty GitHub repository and push it
  • Keep tokens and keys out of chats, emails and repositories

Why your password doesn't work

Anyone may read a public repository, but only its owner and the people they invite (lesson 5.1) may push. So when Git pushes, GitHub needs to know it's you.

Your GitHub password doesn't work in Git. GitHub stopped accepting account passwords for Git operations, and with two-factor authentication a password alone wouldn't be enough anyway. Instead, your computer proves who you are in one of two ways, set up once:

  • HTTPS addresses (https://github.com/ana-reyes/kusina.git), with a credential manager that signs you in through your browser.
  • SSH addresses ([email protected]:ana-reyes/kusina.git), with a key pair.

Either works. GitHub's own guides are the place to follow, because the exact steps change from time to time; this lesson explains what they're doing.

HTTPS: a credential manager

Git Credential Manager comes with Git for Windows, and can be installed on macOS and Linux. The first time you push to an HTTPS address, it opens GitHub's sign-in page in your browser. You sign in there, with your password and second factor, and it stores a token: a long, generated password that only lets Git talk to GitHub. After that, pushes just work.

The GitHub CLI does the same job: run gh auth login, answer its questions, and it signs Git in.

If something ever asks for a "password" when you push, it means a personal access token, made in GitHub's Settings › Developer settings. Prefer a fine-grained token, limited to the repositories it needs, with an expiry date. A token is a password: it goes into a credential manager, never into a chat, an email, or a file in a repository.

SSH: a key pair

An SSH key comes in two halves, made together on your computer with ssh-keygen:

  • The private key stays on your computer, in ~/.ssh. It's like the key to your front door: never share or copy it anywhere.
  • The public key is like the lock. You paste it into GitHub's Settings › SSH and GPG keys. It's safe to share.

When you push, your computer proves it holds the private key that matches the lock, without sending the key. ssh -T [email protected] tests the connection: GitHub replies with your username.

Keys don't expire unless you say so; protect yours with a passphrase, and delete its public half from GitHub if you lose the computer.

Connect Kusina and push

On GitHub, make a new, empty repository (lesson 4.2: no README, .gitignore or licence). GitHub then shows a quick-setup page:

  1. Address. The repository’s address, as HTTPS or SSH, matching how you set up sign-in.
  2. A new repository. For starting from nothing. Not Ana’s case.
  3. An existing repository. Ana’s case: connect her Kusina to this one as origin, make sure her branch is called main, and push it with its whole history.
The quick-setup page of an empty GitHub repository. The course’s own drawing of this GitHub screen, with GitHub’s names for each part. A screenshot from the course’s demo account will replace it.

git branch -M main renames the current branch to main; Kusina's already is (lesson 1.4). Try the other two in the sandbox, where the empty repository is waiting at a pretend address:

Git sandbox
  1. Check there are no remotes yet: git remote -v prints nothing
  2. Add the empty hosted repository as origin (its address is in the note above)
  3. Push main, setting its upstream
  4. Check git status: up to date with origin/main

Working folder

no changes

Staging area

nothing staged

Repository

10 commits on main

You are in ~/kusina. An empty repository is waiting on the hosting site at https://example.com/ana/kusina.git.

Commit graph

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe

Refresh GitHub after a real push, and Kusina's files, README and every commit are there, with the same hashes as on your computer.

Other ways to do the same

GitHub Desktop and the GitHub CLI (gh) can do all of this too: gh repo create makes the GitHub repository and pushes in one command, and GitHub Desktop has a Publish repository button. They run the same Git commands underneath, so what you learned here explains what they're doing, and what to do when they show an error.

Public or private? Kusina is family recipes, so Ana makes it public. Everything pushed is history, old commits included: check what's in it first, as in lesson 4.3.

Spot the trap: a token in the chat

Spot the trapJoaquin

Joaquin can’t get his push to work. He makes a token on GitHub, and pastes it into the family group chat: “Ana, can you try with this?”

What happens if…

Check what you learned

Git asks for a password when Ana pushes to GitHub over HTTPS. What does it actually want?
With SSH keys, which half goes onto GitHub?
Ana’s Kusina already has commits. Which commands from the quick-setup page does she need?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git