Sub topic 3
Using a personal access token over HTTPS
This chapter assumes you can clone, commit, and push, and that you know what a remote URL is. A personal access token (PAT) is a generated string that replaces your account password when Git talks to GitHub over HTTPS. GitHub no longer accepts account passwords for Git operations.
How the token fits into an HTTPS request
When a command such as git push contacts an HTTPS remote, Git asks for a username and a password. You enter your GitHub username. For the password, you enter the token. GitHub checks the token and then decides what that token may do.
Creating a token
- Open GitHub, then Settings, then Developer settings, then Personal access tokens.
- Choose a fine-grained token if your use case supports it. Fine-grained tokens can be limited to chosen repositories and specific permissions. Classic tokens use broad scopes such as
repo. - Set an expiration date and give the token a name that says where it is used.
- For a fine-grained token, grant only the Contents permission with read and write access for the repositories you push to.
- Copy the token immediately. GitHub shows it only once.
Using the token
Check that the remote is an HTTPS URL, then run a command that needs authentication:
git remote -v
git clone https://github.com/OWNER/REPO.gitWhen Git prompts, enter the following:
- Username: your GitHub username.
- Password: the token, not your account password.
Pasted input is usually hidden in a password prompt, so nothing appears on screen. That is normal.
Common errors
| Message | Likely cause | Fix |
|---|---|---|
| `Password authentication is not supported for Git operations` | You entered your account password. | Enter the token instead. |
| `Authentication failed` | The token is expired, revoked, or mistyped. | Create a new token and retry. |
| `Permission to OWNER/REPO.git denied` or a 403 error | The token does not cover this repository or lacks write permission. | Edit the token's repository access and permissions. |
| `Repository not found` on a private repository | The token cannot see the repository. GitHub hides repositories you cannot access. | Add the repository to the token. |
Typing the token at every push is tedious. The next chapters cover how to store it safely. Until then, Git will ask each time unless a credential helper is already configured.
Exercise
You run git push over HTTPS and Git rejects your account password. You create a fine-grained token, but the push now fails with a 403 error. What should you check on the token?
Show answer
Check that the token's repository access includes this repository and that its Contents permission is set to read and write. A token without write access to the repository can authenticate but still be denied the push.