Push the Site With a Token

The store’s repository on GitHub is still empty, and your laptop knows where it lives. In the last lesson, the check report even named the command that fills it, git push -u origin main. So far, GitHub has never heard of your laptop, though, and it isn’t going to accept the store’s whole history from a computer it doesn’t know. With the starter project, that history is 22 commits.

In this lesson, you’ll push the site’s whole history to GitHub and connect your main branch to the one there. On the way, you’ll find out what GitHub accepts as proof that the push is yours, and you’ll create a token that can push to this one repository and nothing else. Start with the command the check report suggested and see what GitHub asks for.

Push and Meet GitHub’s Sign-In

A push sends the commits that your branch has and the remote’s branch doesn’t, and then moves the remote’s branch to match. It moves an existing branch only when the new commits build on everything that branch already has, so a plain push never throws away commits that only the remote has.

GitHub’s main doesn’t exist yet, so this first push sends everything. In the terminal in your generic-grocery folder, run the command from the report:

Language: Shell
$ git push -u origin main

Reading your public repository works without signing in, but changing it doesn’t, so GitHub asks who you are before it takes anything. What you see next depends on your system:

Git for Windows hands the question to Git Credential Manager, a helper that the installer sets up with its default options. A window titled Connect to GitHub opens. Its first tab has a highlighted Sign in with your browser button, and next to that tab there’s a second one called Token.

Before you click anything, look at both options. Which one would you click?

Whichever you picked, don’t click anything yet. Leave the window open for now, because the Token tab is where you’ll sign in instead. The window doesn’t even offer a place for your GitHub password, and the rest of this lesson explains why and what goes in that tab.

Git asks for a username and then for a password. Answer both the way you’d sign in to GitHub, and press Enter after each. The terminal doesn’t show anything while you type the password, so type carefully. Here’s how that looks with the store’s GitHub login:

Language: Shell
$ git push -u origin main
Username for 'https://github.com': generic-grocery-dev
Password for 'https://[email protected]':
remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for 'https://github.com/generic-grocery-dev/generic-grocery.git/'

Git asks for a username and then for a password. Answer both the way you’d sign in to GitHub, and press Enter after each. The terminal doesn’t show anything while you type the password, so type carefully. Here’s how that looks with the store’s GitHub login:

Language: Shell
$ git push -u origin main
Username for 'https://github.com': generic-grocery-dev
Password for 'https://[email protected]':
remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for 'https://github.com/generic-grocery-dev/generic-grocery.git/'

Most people go for the highlighted button, because it looks like the way to sign in. It would work, but it gives Git Credential Manager access to every repository on your GitHub account, with permission to change all of them. That’s far more than pushing one site needs.

On macOS and Linux, Git refused the push. Take a closer look at the line that starts with remote:, because that’s GitHub talking. It doesn’t say the password was wrong. It says that password authentication isn’t supported for Git operations at all.

GitHub stopped accepting account passwords for Git in August 2021. Your password still signs you in on the website, but when Git pushes, GitHub wants something that can be limited and taken back without touching your account. Git’s only role is to pass your answers along, which is why the prompt still says Password.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.