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:
- Address. The repository’s address, as HTTPS or SSH, matching how you set up sign-in.
- A new repository. For starting from nothing. Not Ana’s case.
- 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.
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:
- Check there are no remotes yet:
git remote -vprints nothing - Add the empty hosted repository as
origin(its address is in the note above) - Push main, setting its upstream
- 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
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add 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
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
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.
- GitHub Docs: Caching your GitHub credentials in Git: GitHub CLI and Git Credential Manager
- GitHub Docs: About remote repositories: HTTPS and SSH addresses, and tokens in place of passwords
- GitHub Docs: Connecting to GitHub with SSH: making a key and adding it to your account
- GitHub Docs: Managing your personal access tokens: fine-grained tokens and expiry
- GitHub Docs: Creating a new repository: an empty repository for an existing project
- Pro Git: Credential Storage: how Git remembers HTTPS sign-ins