我想在 Git Extensions、Sourcetree 或其他 Git GUI 中自动使用 push 和 pull,无需每次手动输入用户名和密码。
那么我该如何保存我的 Git 凭据呢?
我想在 Git Extensions、Sourcetree 或其他 Git GUI 中自动使用 push 和 pull,无需每次手动输入用户名和密码。
那么我该如何保存我的 Git 凭据呢?
注意:该方法会将凭据以明文形式保存在您计算机的磁盘上。您计算机上的任何人都可以访问它,例如恶意 NPM 模块。
运行
git config --global credential.helper store
然后
git pull
提供用户名和密码,这些详细信息将在以后被记住。凭证存储在磁盘上的文件中,磁盘权限为“仅用户可读/可写”,但仍然是明文。
如果您想稍后更改密码
git pull
由于密码不正确,将失败,然后git会从~/.git-credentials
文件中移除有问题的用户+密码,所以现在重新运行。
git pull
提供一个新密码,以便它能像之前一样工作。
--global
,仅为一个存储库(在拉取和推送时)存储密码,而不是为任何其他存储库存储密码(这些存储库可能位于不同的主机上,具有不同的凭据,由于某种原因可能不想存储)。 - pseyfert~/.git-credentials
的保护和~/.ssh/id_rsa
这样的私钥没有什么不同。因此,如果您的私钥没有密码,那么~/git-credentials
并不比SSH密钥更糟糕。 - Jason Sgit config
在Git中启用凭据存储。git config --global credential.helper store
运行此命令时,第一次从远程代码库pull或push时,将询问您的用户名和密码。git config credential.helper 'cache --timeout=<timeout>'
该方法可选一个超时参数
,以确定凭据在内存中保留的时间长度。使用此助手,凭据将永远不会触及磁盘,并将在指定的超时时间后被擦除。 默认值
为900秒(15分钟)。
警告:如果您使用此方法,您的Git账户密码将以明文格式保存在全局.gitconfig文件
中,例如,在Linux中为/home/[用户名]/.gitconfig
。
如果您不希望这样做,请改用ssh密钥
来管理您的账户。
git config credential.helper cache
,密码不会保存到文件中,只会存储在内存中。请参见:https://git-scm.com/docs/git-credential-cache - S.A.按照以下步骤生成密钥:更多详情
$ssh-keygen -t rsa -b 4096 -C "yourEmail@something.com"
$ clip < ~/.ssh/id_rsa.pub
ssh-add -K ~/.ssh/id_rsa
我们可以使用git-credential-cache来缓存我们的用户名和密码一段时间。只需在您的CLI(终端或命令提示符)中输入以下内容:
git config --global credential.helper cache
git config --global credential.helper 'cache --timeout=3600'
〜/ .ssh / id_rsa
中,与〜/ .git-credentials
一样不安全?同时,credential.helper存储方法不会将密码/令牌放入.gitconfig文件中。另外,由于GitHub等网站的特殊性,您可以对个人访问令牌(PAT)拥有更细粒度的权限控制,而不是SSH公钥。因此,对于这种特定情况,我认为使用凭证助手的PAT更加“安全”。 - Jason S在终端中输入以下内容:
# 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 帮助中心。
--timeout
参数的最大值是多少? - Aleksandar~/.gitconfig
文件来存储你的凭据。nano ~/.gitconfig
应该已经有
[user]
email = your@email.com
user = gitUSER
[credential]
helper = store
~/.git-credentials
的格式保存。https://gituser:gitpassword@domain.xxx
DOMAIN.XXX 可能是 github.com、bitbucket.org 或其他网站
请参阅文档。
exec bash
。 - Timo在查阅了数十篇Stack Overflow帖子、博客等资料后,我尝试了每一种方法,最终得出了这个方案。它涵盖了所有内容。
以下是您可以安全地使用的所有方式和工具,通过它们可以进行Git身份验证以克隆存储库而无需交互式密码提示。
想要Just Works™吗?这就是神奇的银弹。
获取您的访问令牌(如果需要GitHub或Gitea的说明,请参见备忘单中的相应部分),并在环境变量中设置它(用于本地开发和部署):
MY_GIT_TOKEN=xxxxxxxxxxxxxxxx
对于 GitHub,复制并粘贴以下这些代码 verbatim(逐字翻译):
git config --global url."https://api:$MY_GIT_TOKEN@github.com/".insteadOf "https://github.com/"
git config --global url."https://ssh:$MY_GIT_TOKEN@github.com/".insteadOf "ssh://git@github.com/"
git config --global url."https://git:$MY_GIT_TOKEN@github.com/".insteadOf "git@github.com:"
没有使用GitHub?
对于其他平台(Gitea、GitHub和Bitbucket),只需更改URL即可。不要更改用户名(虽然是任意的,但它们需要用于不同的配置条目)。
兼容性
这适用于macOS、Linux、Windows(在Bash中)、Docker、CircleCI、Heroku、Akkeris等本地环境。
更多信息
请参见抄袭表的“.gitconfig insteadOf”部分。
安全性
请参见抄袭表的“安全”部分。
git config --global credential."https://somegithost.com".username MyUserName
这个命令,它在你的备忘录中有提到,但在帖子的其他地方却没有。这个特定的解决方案并没有回答楼主的问题,但它回答了我的问题,所以谢谢! - TheDudeAbides我认为缓存凭据比永久存储凭据更安全:
git config --global credential.helper 'cache --timeout=10800'
现在你可以输入你的用户名和密码(git pull
或者...),并且在接下来的三个小时内继续使用Git。
非常好而且安全。
超时时间单位为秒(本例中为三小时)。
要进行全局设置,请打开终端(无论在何处),运行以下命令:
git config --global user.name "your username"
git config --global user.password "your password"
因此,您在计算机上拥有的任何本地Git存储库都将使用该信息。
您可以通过执行以下操作为每个存储库单独配置:
在存储库文件夹中打开终端。
运行以下命令:
git config user.name "your username"
git config user.password "your password"
它仅影响该文件夹(因为您的配置是本地的)。
--local
标签,但仍然没有反应。 - PayamB.git config --global
仍然只能由用户在~/.gitconfig
中访问,因此从这个意义上说,它可能不比您家目录中没有密码保护的私有ssh密钥(或类似于aws cli的~/.aws/config
)更糟糕。如果您对git config使用了--system
,它将对计算机上的所有用户可用,因此最好不要这样做,也没有必要,尽管它仍然仅限于计算机上的其他用户。 - Jason Suser.name
用于在编写提交信息时表示作者的全名。这与用于身份验证远程服务器的用户名无关。而且,绝不能将密码存储在配置文件中(这是 git-config 的作用)。 - undefinedhttps://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
git@github.com:Username/myRepo.git
- Loichhttps
克隆 Wiki,而不能通过 ssh
,因此这样的解决方案可能会有用。另外,请注意您可以使用 oauth-token,这比您的 GitHub 密码稍微更安全一些。 - AstroFloyd您可以使用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 - 在磁盘上存储凭据的助手。