Using Personal Access Tokens with GitHub and Git
There are situations where accessing GitHub through SSH may not be possible, for
example, when a firewall or proxy is blocking access. One way of working with
this situation is to access GitHub using HTTP/HTTPS while still using the local
git
client. This article provides some information on how to access GitHub
with the git
command-client over HTTPS.
GitHub Documentation
Before beginning, I would point you to the GitHub Documentation on Authentication. Specifically, it is worth looking at the section on authenticating with the command line.
In the section on authenticating with the command line, it describes how to connect via HTTPS. GitHub encourages you to use their GitHub CLI client or the Git Credential Manager to help manage logins. Below I will quickly outline how to use a personal access token to perform the authentication without additional software.
Creating a Personal Access Token
As noted in the GitHub documentation on creating a personal access token, the tokens are effectively sensitive passwords. You don’t want to put them in places where others can get to them. Using a password manager for the token would make some sense or using the GitHub CLI client or the Git Credential Manager mentioned above would reduce some of the security concerns.
Following the information for the link above, I created a "Tokens (classic)"
token since they can be used with Git to authenticate over HTTPS. GitHub
recommends using an expiration date for a token to limit the damage if it is
stolen or otherwise leaked. I used the 90-day token for this example and
limited the scope of the token to repo
since we are just trying to use Git
over HTTPS.
❗
|
Once you hit the Generate button, you will be given the token as a
string. This is your only chance to record the token so copy it into a safe
place for later use. If you lose it, you will have to generate a new token.
|
I used a KeePass tool (KeePass, KeePassXC, etc.) to store my personal access token for future use. With a password manager, it will be a little easier to copy the token and use it on the command line then typing it by hand frequently.
Using the Personal Access Token with Git and GitHub
At this point, you can clone the repository by going to the repository in GitHub and using the HTTPS URL to access the repository, for example:
https://github.com/myusername/myusername.github.io.git
You can then modify the repository as usual (git add
, git commit
, etc.).
Then, when you want to git push
the repository, it will ask you for your
GitHub username and then your GitHub password. For the password, use the
recently created GitHub personal token.
Pushing an New Repository to GitHub
If you are trying to push an existing repository to GitHub, you would, for example, use something like the following to set the remote origin on GitHub:
git remote add origin https://github.com/myusername/myusername.github.io.git
before you do the final:
git push -u origin main
to push the new repository to GitHub. At this point, it should ask for your GitHub username and password. The password, of course, is your personal access token created earlier.