git push using GitHub token [Deprecating password authentication]

I am using git for version control and GitHub for the repository of my code development. Every git push of my work to the repository is prompted to input my GitHub username and password. I would like to avoid it for every push, but how to setup git push to use GitHub token.

Solution:

Password-based authentication for Git is deprecated and you should make the push based on the token authentication. GitHub has personal access token (PAT), to use in place of a password with the command line or with the API. Below is how to generate the token and use it:

Create a token in GitHub

  1. Log in to GitHub and navigate to the Settings page as shown below:

GitHub settings menu

2. Click on Developer Settings

GitHub Developer Settings

3. Click on Personal Access Tokens

How to create personal access token in GitHub

4. Click on Generate new token

git push using GitHub token

5. Now type in the name of the token and select the scopes, or permissions, you’d like to grant this token. Make sure you select repo to use your token to access repositories from the command line. Click Generate token.

Scope for the token

Note:

Make sure to copy your personal access token immediately. You won’t be able to see it again!

How to git push using GitHub token on the command line

Note:

Personal Access Tokens (PAT) can only be used for HTTPS git operations. Switch your repository to HTTPS if it is using SSH.

Once you have a token, you can use it instead of your password when performing git push operations over HTTPS as below:

git push https://<GITHUB_ACCESS_TOKEN>@github.com/<GITHUB_USERNAME>/<REPOSITORY_NAME>.git

Note: Remember to replace <GITHUB_ACCESS_TOKEN>, <GITHUB_USERNAME>, <REPOSITORY_NAME> with your token, username and repository.

Updated on September 6, 2021

Was this article helpful?

Related Articles

Comments

  1. The most helpful article without unnecessary info, thx !

  2. This does not work.
    I have generated Personal token with full access. It still looking for aditional token to enter as a password. Something is wrong here.

  3. Thank you so much, that was very confusing as the command line kept asking for my username and password only to tell me that method has been discontinued.

  4. git push https://@github.com/.git @ 22/09/2021 18:16 is how I managed to push my repository in github. Didin’t work with my username between my token and the repository Thanks a million though, you brought me half way through!!

  5. for the first time I have got my solution in only one search.

  6. Thanks very much Ramya Santhosh. This worked beautifully. I also don’t understand why Github didn’t explain this in alerts, as my immediate concern was being able to push like before. Good point noted above that if pushing to someone else’s account one would have to enter their account instead of the account of the person pushing.

    My inexpert suspicion is that some of the “account security” features Microsoft is making me set up are good for Microsoft and might be desirable for some users but aren’t necessarily things I want or need.

  7. Thanks so very much for this Ramya.

    The words “enter it instead of your password” is the golden phrase missing from a lot of material.

    I share Vincent Flair’s frustration.

    Keep up the good work

  8. Thank you. Clearest and easiest solution I have seen. I don’t know why this is not part of the alerts that github keeps sending out. One potential wrinkle — if pushing to a repository of someone else in the team, the should be of the account under which exists.

Leave a Comment