命令行或API上使用Git通过HTTPS执行Git操作时,可以创建一个个人访问令牌并用它代替密码。
A personal access token is required to authenticate to GitHub in the following situations:
- When you're using two-factor authentication
- To access protected content in an organization that uses SAML single sign-on (SSO). Tokens used with organizations that use SAML SSO must be authorized.
As a security precaution, GitHub automatically removes personal access tokens that haven't been used in a year.
Creating a token
-
验证您的电子邮箱地址,(如果尚未验证)。
-
在任何页面的右上角,单击您的个人资料照片,然后单击设置。
-
在左侧边栏中,单击“开发人员设置”。
-
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.
-
Click to 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.
Warning: Treat your tokens like passwords and keep them secret. When working with the API, use tokens as environment variables instead of hardcoding them into your programs.
-
To use your token to authenticate to an organization that uses SAML SSO, authorize the token for use with a SAML single-sign-on organization.
Using a token on the command line
Once you have a token, you can enter it instead of your password when performing Git operations over HTTPS.
For example, on the command line you would enter the following:
$ git clone https://github.com/username/repo.git Username: your_username
Password: your_token
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.
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.