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

问题描述 投票:624回答:11

我想在GitExtension中自动使用push和pull,而不是每次都在提示符中输入我的用户名和密码。

那么如何在GIT中保存我的凭据呢?

git credentials git-config
11个回答
1200
投票

git config credential.helper store

然后

git pull

提供用户名和密码,稍后将记住这些详细信息。凭据存储在磁盘上的文件中,磁盘权限为“仅用户可读/可写”,但仍为明文。

如果您想稍后更改密码

git pull

将失败,因为密码不正确,git然后从~/.git-credentials文件中删除有问题的用户+密码,所以现在重新运行

git pull

提供一个新密码,使其像以前一样工作。


10
投票

在这种情况下,您需要git凭证帮助器通过使用以下命令行告诉git记住您的GitHub密码和用户名:

git config --global credential.helper wincred 

如果您使用SSH密钥使用repo,则需要使用SSH密钥进行身份验证。


2
投票

除了编辑~/.gitconfig文件外,如果你问:

git config --local --edit

要么

git config --global --edit

Note to always use single quotes:

git config --local user.name 'your username'
git config --local user.password 'your password'

要么

git config --global user.name 'your username'
git config --global user.password 'your password'

如果您使用双引号,您的用户名和密码可能会使用一些会破坏您密码的字符。

--local--global表示为项目或os用户保存配置参数。


0
投票

来自rifrol的评论,在Linux Ubuntu上,来自这个answer

sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

其他一些发行版提供了二进制文件,因此您无需构建它。

在OS X中,它通常使用“osxkeychain”的默认模块“构建”,因此您可以免费获得它。


216
投票

您可以使用git config在git中启用凭据存储。

git config --global credential.helper store

运行此命令时,第一次从远程存储库中提取或推送时,系统会询问您的用户名和密码。

之后,为了与远程存储库进行后续通信,您不必提供用户名和密码。

存储格式是.git-credentials文件,以纯文本格式存储。

此外,您可以使用git config credential.helper的其他帮助程序,即内存缓存:

git config credential.helper cache <timeout>

需要一个可选的timeout parameter,确定它的守护进程应该运行多长时间,它的default值是900 seconds (15 minutes).


警告:如果您使用此方法,您的git帐户密码将以plaintext格式保存在global .gitconfig file中,例如在linux中它将是/home/[username]/.gitconfig

如果这对您不利,请使用ssh key代替您的帐户。


74
投票

Turn on the credential helper so that Git will save your password in memory for some time:

在终端中,输入以下内容:

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

默认情况下,Git会将您的密码缓存15分钟。

要更改默认密码缓存超时,请输入以下内容:

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

来自GitHub Help


72
投票

您可以像这样设置用户名和密码:

git config --global user.name "your username"

git config --global user.password "your password"

30
投票

您可以编辑~/.gitconfig文件以存储您的凭据

sudo nano ~/.gitconfig

应该已经有了

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

您应该在此文件的底部添加。

[credential]
        helper = store

我推荐这个选项的原因是因为它是全局的,如果在任何时候你需要删除选项,你知道去哪里并改变它。

仅在您的个人计算机中使用此选项。

然后当你拉|克隆|输入git密码,一般来说,密码将以~/.git-credentials的格式保存

https://GITUSER:[email protected]

在哪里DOMAIN.XXX可能是GITHUB.COM | BITBUCKET.ORG |其他

Docs

Restart your terminal.


16
投票

只需将您的凭据放入Url中,如下所示:

https://Username:@ Password github.com/myRepoDir/myRepo.git

你可以这样存储它:

git remote add myrepo https://Userna...

...使用它的示例:

git push myrepo master


现在就是列出url别名:

git remote -v

...以及删除其中一个的命令:

git remote rm myrepo


14
投票

您可以使用git-credential-store将未加密的密码存储在磁盘上,仅受文件系统权限的保护。

$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

[several days later]
$ git push http://example.com/repo.git
[your credentials are used automatically]

您可以检查存储在文件~/.git-credentials中的凭据

欲了解更多信息,请访问git-credential-store - Helper to store credentials on disk


14
投票

如果使用SSH身份验证而不是用户名/密码身份验证,则会更安全。

如果您使用的是Mac,则SSH客户端身份验证将集成到MacOS钥匙串中。创建SSH密钥后,键入终端:

ssh-add -K ~/.ssh/id_rsa

这会将SSH私钥添加到MacOS钥匙串中。 git客户端在连接到远程服务器时将使用ssh。只要您在服务器上注册了ssh公钥,就可以了。


14
投票

对于全局设置,打开终端(从任何地方)运行以下:

  git config --global user.name "your username"
  git config --global user.password "your password"

因此,您在计算机上拥有的任何本地git仓库都将使用该信息。

您可以通过执行以下操作为每个仓库单独配置:

  • 在repo文件夹中打开终端。
  • 运行如下: git config user.name "your username" git config user.password "your password"

它仅影响该文件夹(因为您的配置是本地的)。

© www.soinside.com 2019 - 2024. All rights reserved.