如何在Git中保存用户名和密码?

本文翻译自:How to save username and password in Git?

I want to use a push and pull automatically in GitExtension , without entering my user and password in a prompt, every time. 我想在GitExtension中自动使用推拉操作 ,而不必每次都在提示中输入用户名和密码。

So how can I save my credentials in GIT ? 那么如何将我的凭据保存在GIT中呢?


#1楼

参考:https://stackoom.com/question/2QoLq/如何在Git中保存用户名和密码


#2楼

Run

git config --global credential.helper store

then 然后

git pull

provide a username and password and those details will then be remembered later. 提供用户名和密码,这些详细信息将在以后被记住。 The credentials are stored in a file on the disk, with the disk permissions of "just user readable/writable" but still in plaintext. 凭据存储在磁盘上的文件中,磁盘权限为“仅用户可读/可写”,但仍为纯文本格式。

If you want to change the password later 如果您以后想要更改密码

git pull

Will fail, because the password is incorrect, git then removes the offending user+password from the ~/.git-credentials file, so now re-run 将会失败,因为密码不正确,然后git从~/.git-credentials文件中删除有问题的用户+密码,因此现在重新运行

git pull

to provide a new password so it works as earlier. 提供一个新密码,使其可以更早地工作。


#3楼

You can use the git config to enable credentials storage in git. 您可以使用git config启用git中的凭据存储。

git config --global credential.helper store

When running this command, the first time you pull or push from the remote repository, you'll get asked about the username and password. 运行此命令时,第一次从远程存储库中拉出或推送时,系统会询问您用户名和密码。

Afterwards, for consequent communications with the remote repository you don't have to provide the username and password. 之后,为了与远程存储库进行后续通信,您无需提供用户名和密码。

The storage format is a .git-credentials file, stored in plaintext. 存储格式是.git-credentials文件,以纯文本格式存储。

Also, you can use other helpers for the git config credential.helper , namely memory cache: 另外,您可以将其他帮助程序用于git config credential.helper ,即内存缓存:

git config credential.helper cache <timeout>

which takes an optional timeout parameter , determining for how long the credentials will be kept in memory. 它带有一个可选的timeout parameter ,用于确定凭据将在内存中保留多长时间。 Using the helper, the credentials will never touch the disk and will be erased after the specified timeout. 使用帮助程序,凭据将永远不会接触磁盘,并且在指定的超时后将被删除。 The default value is 900 seconds (15 minutes). default值为900 seconds (15 minutes).


WARNING : If you use this method, your git account passwords will be saved in plaintext format, in the global .gitconfig file , eg in linux it will be /home/[username]/.gitconfig 警告 :如果使用此方法,则您的git帐户密码将以plaintext格式保存在global .gitconfig file ,例如在linux中将为/ /home/[username]/.gitconfig

If this is undesirable to you, use an ssh key for your accounts instead. 如果您不希望这样做,请为您的帐户使用ssh key


#4楼

Turn on the credential helper so that Git will save your password in memory for some time: 打开凭据助手,以便Git将您的密码保存在内存中一段时间​​:

In Terminal, enter the following: 在终端中,输入以下内容:

# Set git to use the credential memory cache
git config --global credential.helper cache

By default, Git will cache your password for 15 minutes. 默认情况下,Git会将您的密码缓存15分钟。

To change the default password cache timeout, enter the following: 要更改默认密码缓存超时,请输入以下内容:

# Set the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'

From GitHub Help GitHub帮助


#5楼

You will be more secure if you use SSH authentication than username/password authentication. 如果您使用SSH身份验证,则比用户名/密码身份验证更安全。

If you are using a Mac, SSH client authentication is integrated into the MacOS keychain. 如果您使用的是Mac,则SSH客户端身份验证已集成到MacOS钥匙串中。 Once you have created an SSH key, type into your terminal: 创建SSH密钥后,在终端中输入:

ssh-add -K ~/.ssh/id_rsa

This will add the SSH private key to the MacOS keychain. 这会将SSH私钥添加到MacOS钥匙串中。 The git client will use ssh when it connects to the remote server. git客户端连接到远程服务器时将使用ssh。 As long as you have registered your ssh public key with the server, you will be fine. 只要您在服务器上注册了ssh公钥,就可以了。


#6楼

You can edit the ~/.gitconfig file to store your credentials 您可以编辑~/.gitconfig文件来存储您的凭据

sudo nano ~/.gitconfig

Which should already have 哪个应该已经有

[user]
        email = [email protected]
        user = gitUSER

You should add at the bottom of this file. 您应该在此文件的底部添加。

[credential]
        helper = store

The reason I recommend this option is cause it is global and if at any point you need to remove the option you know where to go and change it. 我之所以建议使用此选项,是因为它是全局的,如果您随时需要删除该选项,则知道该去哪里进行更改。

ONLY USE THIS OPTION IN YOU PERSONAL COMPUTER. 仅在个人计算机中使用此选项。

Then when you pull | 然后当你拉| clone| 克隆| enter you git password, in general, the password will be saved in ~/.git-credentials in the format 输入您的git密码,通常,密码将以~/.git-credentials格式保存

https://GITUSER:[email protected]

WHERE DOMAIN.XXX COULD BE GITHUB.COM | DOMAIN.XXX可能位于GITHUB.COM | BITBUCKET.ORG | BITBUCKET.ORG | OTHER 其他

See Docs 查看文件

Restart your terminal. 重新启动终端。

发布了0 篇原创文章 · 获赞 7 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105378859