Get startedGet started for free

Personal Access Tokens (PAT)

1. Personal Access Tokens (PAT)

Now we've seen how to work with private repos let's go further by learning about Personal Access Tokens.

2. Authentication

Recall that we can use Git in the terminal to interact with GitHub. For example, say we want to clone the bank marketing repo locally. In the terminal, we run git clone followed by the repo's URL. We are prompted for our username, and provide it. Git also asks for our password, which we provide. Note that the terminal won't display this.

3. Authentication failed

We get an output telling us that password authentication was removed on August 13th 2021, along with a link to documentation for more guidance.

4. GitHub Docs

Visiting this URL, we see that when we perform Git commands in the terminal to interact with a remote repo such as clone, fetch, pull, or push, and are prompted for our password, we actually need to provide a personal access token.

5. What is a Personal Access Token?

So, what is a Personal Access Token, or PAT? A PAT is an alternative to a password when authentication is required for terminal commands. As the output showed us earlier, it is required for interacting with remote repos since August 2021. They are used because they are more secure than entering a password. Note that a PAT is not required when using GitHub, only when interacting with a remote repo via the terminal. We need to create a PAT to clone the bank marketing repo, so let's see how to do this.

6. Creating a PAT

The first step is to go to our account settings, which we can access from our profile.

7. Creating a PAT

In our settings page, we need to scroll down to the bottom. Here, we see an option called Developer settings in the bottom left.

8. Creating a PAT

Clicking it opens a page with three options: GitHub Apps, OAuth Apps, and Personal access tokens. The first two are out of scope for this course, so let's click on Personal access tokens.

9. Creating a PAT

We see a button called Generate new token. This looks promising!

10. Creating a PAT

We can now see several settings for the PAT. We first enter a Note, which is the name GitHub assigns to our PAT - we call ours bank marketing PAT. We can also set the Expiration time, after which the token won't work. By default it is 30 days, but we can also set it to 7, 60, or 90 days, or a custom value. We'll leave it as-is in this instance.

11. Creating a PAT

The next section is to Select the scope of the PAT. This determines what the PAT allows us to do. There are a lot of options, so we'll just focus on repo access. By checking the repo box, our PAT will have full access to our private repos, which is sufficient for our needs.

12. Creating a PAT

Scrolling to the bottom of the page, we can click the Generate token button.

13. Using the PAT

This reloads the page with a prompt to make sure we copy our PAT as we won't be able to see it again.

14. Using the PAT

Underneath this message is the PAT, with letters and numbers. We can click the clipboard icon to copy it. It's important to do this, as if we refresh this page we'll no longer see the PAT or be able to copy it. We also shouldn't share our PAT with anyone else!

15. Using the PAT

Returning to the terminal, we try again to clone our private repo. This time, when prompted for our password, we paste our PAT. We see a different output this time. Running ls, we see the bank_marketing repo exists locally!

16. Let's practice!

Now it's your turn to work with personal access tokens!