Git Push 避免用户名和密码方法
在大家使用github的过程中,一定会碰到这样一种情况,就是每次要push 和pull时总是要输入github的账号和密码,这样不仅浪费了大量的时间且降低了工作效率。在此背景下,本文在网上找了两种方法来避免这种状况,这些成果也是先人提出来的,在此只是做个总结。
方法一 Git Credential Manager 【推荐】
方法二 .git-credentials
1.1 创建文件存储GIT用户名和密码
在%HOME%目录中创建文件名为.git-credentials的文件。
%HOME%目录:
- Windows:一般为
C:\users\Administrator
,也可以是你自己创建的系统用户名目录,反正都在C:\users\中。 - Mac:/Users/YourUserName
1 | cd ~ |
1 | # 输入 https://{username}:{password}@github.com |
1.2 添加Git Config 内容
进入终端, 输入如下命令:
1 | git config --global credential.helper store |
执行完后查看%HOME%目录下的.gitconfig文件,会多了一项:
1 | [credential] |
重启终端会发现git push时不用再输入用户名和密码。
方法三
How to handle multiple git configurations in one machine
Setting Up Git Identities
方法三 环境变量
2.1 添加环境变量
添加一个HOME环境变量,变量名:HOME,变量值:%USERPROFILE%
2.2 创建git用户名和密码存储文件
进入%HOME%目录,新建一个名为"_netrc"的文件,文件中内容格式如下:
1 | machine {git account name}.github.comlogin your-usernmaepassword your-password |
重新打开终端,无需再输入用户名和密码
3 开启两步验证"Two-factor authentication"的github账号
前面用方法一之后, 重启终端, 重新git push
, 跟据提示输入用户名和密码,结果报错:
1 | Username for 'https://github.com': CatherineLiyuankun |
查了下原因,是因为我github账号 开启过两步验证,所以简单说就是不要输入password
而是要输入personal access token
。
可以通过: https://github.com/settings/tokens 查看你账号已有的personal access token。
如果没有可以新建一个personal access token
,并保存下来。
It turns out that I need to log in using my username, but instead of using the normal GitHub account password, I should use a personal access token that can be created here.
Because I am use GitHub feature named “Two-factor authentication” (It isn’t a standard Git’s feature, it’s added feature by GitHub.com). After turn on the feature, I must create “Personal Access Tokens”
Reference
When you’ll be asked for a personal access token as a password
After 2FA is enabled there are a couple of scenarios where you need to enter a personal access tokeninstead of a 2FA code and your GitHub password.
Through the command line
You must create a personal access token to use as a password when authenticating to GitHub on the command line using HTTPS URLs.
For example, when you access a repository using Git on the command line using commands like git clone, git fetch, git pull or git push with HTTPS URLs, you must provide your GitHub username and your personal access token when prompted for a username and password. The command line prompt won’t specify that you should enter your personal access token when it asks for your password.
2FA and Subversion (svn command line, tortoise svn, etc)
When you access a repository via Subversion, you must provide a personal access token instead of entering your password.
附录:配置git账户命令
1 | # 配置git账户 |