Authenticating to GitHub with Personal Access Token

How to create a Personal Access Token on the GitHub website in order to authenticate yourself correctly, so you can perform all kinds of operations, such as cloning repositories or pushing changes to GitHub from local to remote.

In GitHub they have recently declared authentication by means of username and password obsolete and now developers must authenticate us through what they call the Personal Access Token. You must do this to be able to connect and perform any type of operation with the GitHub service.

In the we have hardly talked about GitHub yet, but soon we will get down to operations related to this repository hosting service. However, although it seems that we are skipping some previous steps in addressing this topic now, it is important to explain it, to make it clear how we will access GitHub from the command line terminal, or any other program when we are local. Please, if there is something you don’t understand yet, don’t worry because we will cover it all in due time in the manual.

Why we need the Personal Access Token

Previously, when you wanted to perform any operation on GitHub from local and that operation required authenticated access, either via the command line or a GUI program, you could simply authenticate using the same username and password that you use on the GitHub website. . Now it is not possible and you will have to use the aforementioned Personal Access Token. If you have not yet configured your team to adapt to this change or you are new to GitHub, we explain in this article how to do it currently.

See also  Detect installed PHP versions on Mac

This procedure will have to be done in anywhere you need a key to be able to use GitHub from outside the website. For example, cloning a private GitHub repository locally, pushing changes to GitHub, pulling changes to you, etc.

How to create a Personal Access Token (PAT)

This token is created from the GitHub website. The procedure is very simple. Once logged in with your username and password on the GitHub website, we access the “Settings” option and then “Developer settings” and finally “Personal access tokens”.

The “Settings” option is found in the drop-down menu of your avatar.

The following options are accessed through the vertical navigator on the left. There we press the option “Developer settings” and then “Personal access tokens”.

You will then access a page where you can manage your Personal access tokens.

Here we are going to create a new token with the button “Generate new token“.

Then a form will appear that you must fill in, indicating the name of the token (to know what you are going to use it for) and other details such as the expiration of the token and the “scopes”.

The scopes are simply the operations that will or will not be allowed from this token. The most common is that you give permissions to access the remote repositories hosted on GitHub, but there are other operations that you can do or not and that you could allow or not with this token that you are going to create.

Copy the PAT in a safe place

Once the token is generated it will appear on the GitHub page. You can copy it and put it in a safe place. Keep in mind that it is as if it were a user password! so you must make sure that only you have access to this token.

See also  Difference between CSS margin and padding attributes

Of course, you shouldn’t paste it into the code of any application, so as not to make it visible to other people!! or your access to GitHub could be compromised.

Another important detail is that this token will only appear once on the GitHub page. If you need to recover it because you have lost it, you simply will not be able to do it. You will have to generate a new one.

How to use the GitHub Personal Access Token

The mechanism to use the PAT is as simple as what we previously did with the website key.

Simply, when the GitHub operation asks for your user’s key, you will put the token instead of the key.

For example, in the following image we see the command to clone a repository. When we’re prompted for the key, we simply put the entire token string that we’ve copied to GitHub. With this we will have authenticated with the token correctly and we will be able to access the GitHub services.

Do not try to clone that repository with the command you see in the image, since it is a private repository and it will ask you for the key. Only I have permission to clone it, since it is private. If you want to learn this operation, there is an article that teaches you how to.

How to clear previously stored credentials on GitHub on MacOS

For Mac users, you may need to delete previously stored credentials on your computer. For this you have to open an application called “Keychain Access”.

See also  Scope in Angular, handling scopes with $parent

In it you search for “GitHub” and delete the existing credentials.

After completing this step, when trying to do any operations again with GitHub, your username and password will be requested, and you will enter your username and token, as we have indicated previously.

Caching the Personal Access Token

The Access Token cache can be done on any operating system through Git. In MacOS it is done automatically with the keys application. For users of other operating systems such as Linux and Windows in the GitHub documentation we find some others.

In Windows they tell you that you have to launch the following command:

git config –global credential.helper wincred

In Linux you are informed of two useful commands. This allows you to indicate that the credentials are cached:

git config –global credential.helper cache

And this other command allows you to keep the credentials cached for as long as you deem convenient. The time is indicated in seconds. For example, to cache the credentials for 3 hours you run the following command.

git config –global credential.helper ‘cache –timeout=10800’

That is all! I hope that with these indications you will be able to configure all the GitHub configuration for access from the local terminal and be able to manage your remote repositories.

Loading Facebook Comments ...
Loading Disqus Comments ...