remote: Invalid username or password.

本文介绍如何在启用GitHub两步验证后解决Git推送时出现的身份验证失败问题。通过创建并使用个人访问令牌代替密码进行身份验证,可以轻松解决此问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

$ git push
Username for 'https://github.com': Username
Password for 'https://Username@github.com':
remote: Invalid username or password.
fatal: Authentication failed for 'https://github.com/username/repository.git/'

What causes this error

This error message is self explanatory. It means that we are trying to use the invalid username or password. But I’m sure I’m using the correct username and password. In my case, this happened with me after I turned on a two-factor authentication (2FA) in my GitHub account. So I know what caused this error message.

How to solve it

To solve this problem all we need to do is create a GitHub personal access token and use it instead of our GitHub password and a two-factor authentication code.

Creating a GitHub personal access token.

 

In the upper-right corner of any page, click your profile photo, then click Settings.

In the left sidebar, click Developer settings.

In the left sidebar, click Personal access tokens.

Click Generate new token.

Give your token a descriptive name.

Select the scopes, or permissions, you’d like to grant this token. To use your token to access repositories from the command line, select repo.

Click Generate token.

Copy the token to your clipboard. For security reasons, after you navigate off the page, you will not be able to see the token again.

 

Note! Treat your tokens like passwords and keep them secret (if you don’t want other people to use the API on your behalf). When working with the API, use tokens as environment variables instead of hardcoding them into your programs.

Using a token on the command line.

 

Once we have a token, we can enter it instead of our password when performing Git operations over HTTPS. Just inter your token after prompted a password and then watch the magic happen…

git push
Username: your_username
Password: your_token

Note! Personal access tokens can only be used for HTTPS Git operations. If your repository uses an SSH remote URL, you will need to switch the remote from SSH to HTTPS.

Note! If you are not prompted for your username and password, your credentials may be cached on your computer. You can update your credentials in the Keychain to replace your old password with the token.


If this article helped you solve the problem then please leave a comment:smiley:

Thanks for reading!

转载于:https://my.oschina.net/kun123/blog/1930644

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值