git push using GitHub token [Deprecating password authentication]

Updated on September 6, 2021

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.

Was this article helpful?

Related Articles

Comments Leave a Comment

  1. Thanks a lot, now I’m able to push with this way easily.

  2. It doesn’t help. I still have fatal: Authentication failed for

  3. Thank you very much. You succinctly answered a question I was searching for.

  4. Helpful. Personally I like putting a leading space in front of the command to not have the access token recorded in my history file.

  5. Thank you very much. I was not able to push code, Now I am able to push code using above commnad.

  6. This is completely idiotic.
    This situation is driving me crazy, I generated a token after hours of trying I managed to push my data. Now that it expired I am back to the same situation. Github is shit

  7. Thanks for the solution but is the an alternative way not to use the access token every time you going a push?

  8. thank you for the tutorial. i wish you goodluck, and all is well.

  9. This is a bad practice, to use the token in your URL, as it stores the token as plain text.

  10. It would be useful to see how to configure git to store token instead of writing it each time you push

  11. Thank you ! Worked perfectly !
    I agree with the comments of Sabina Pringle. And I think you tutorial should be included into GitHub’s website.
    Direct and clean.

  12. It is help for a new comer as me, providing the basic of how to use to use the PAT on github.

  13. Thank you! This is only article of many of read that worked. So many articles say you can simply put the token into the place where the traditional cli prompt looks for a password. I’m sure I have pushed to github since passwords were deprecated so maybe github have changed thing again in late 2021? In any case, thanks again.

  14. How do I avoid having to type this god awful token password for every single push?

  15. BLESS YOU! Lots of articles are out there on why this was done. This was the first one I found that told you what to do now. Oh, bless you!!!!!!!!!

    1. This is close but not correct if the repo owner is an organization (or some entity other than GITHUB_USERNAME). In that case, the second instance of GITHUB_USERNAME should be the organization name.

  16. Thank you very mucha; you are the best. I had waste a lot time searching something like this very helpfull article.

  17. my coworker was too stupid to push to our repo but with my help and this article he finaly made it after 45 mins 🙂 thank you

  18. It worked flawlessly. I was banging my head here and there for a day… Thank You Very Much.

  19. Crazy that I had to go this far to find this simple guide

  20. Finally found a perfect solution after pulling my hair for many days!! Thank you!!!

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

  22. 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.

  23. 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.

  24. 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!!

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

  26. 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.

  27. 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

  28. 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