我想在 Git Extensions、Sourcetree 或任何其他 Git GUI 中自动使用推送和拉取功能,而无需每次都在提示中输入我的用户名和密码。
那么如何在 Git 中保存我的凭据呢?
注意:此方法以明文形式将凭据保存在您的 PC 磁盘上。您计算机上的每个人都可以访问它,例如恶意 NPM 模块。
跑
git config --global credential.helper store
然后
git pull
提供用户名和密码,稍后将记住这些详细信息。凭据存储在磁盘上的文件中,具有“仅用户可读/可写”的磁盘权限,但仍以明文形式。
如果您想稍后更改密码
git pull
会失败,因为密码不正确,git然后从~/.git-credentials
文件中删除有问题的用户+密码,所以现在重新运行
git pull
提供一个新密码,以便它像以前一样工作。
您可以使用 git config
在 Git 中启用凭证存储。
git config --global credential.helper store
运行此命令时,第一次从远程存储库中拉取或推送时,系统会询问您用户名和密码。
之后,为了与远程存储库进行后续通信,您不必提供用户名和密码。
存储格式为 .git-credentials
文件,以纯文本形式存储。
此外,您可以为 git config credential.helper
使用其他帮助程序,即内存缓存:
git config credential.helper 'cache --timeout=<timeout>'
这需要一个可选的 timeout parameter
,确定凭据将在内存中保留多长时间。使用帮助程序,凭据将永远不会接触磁盘,并将在指定的超时后被擦除。 default
值为 900 秒(15 分钟)。
警告:如果您使用此方法,您的Git 帐户密码将以纯文本 格式保存在global .gitconfig file
中,例如在Linux 中为/home/[username]/.gitconfig
。
如果您不希望这样做,请改用 ssh key
作为您的帐户。
git config credential.helper cache
,密码不会保存到文件中,只会存储在内存中。请参阅:git-scm.com/docs/git-credential-cache
推荐和安全的方法:SSH
创建 SSH GitHub 密钥。转到 github.com → 设置 → SSH 和 GPG 密钥 → 新 SSH 密钥。现在将您的私钥保存到您的计算机上。
然后,如果私钥保存为 id_rsa 在 ~/.ssh/ 目录中,我们添加它进行身份验证,如下所示:
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 等网站的怪癖,您可以比 SSH 公钥更精细地控制个人访问令牌的权限。所以对于那个特定情况,我会说 PAT凭证助手“更安全”
打开凭证助手,以便 Git 将您的密码保存在内存中一段时间:
在终端中,输入以下内容:
# 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。
cache
部分替换为 store
。因此,完整的命令将是:git config --global credential.helper store
。请注意,这会将您的密码存储在一个开放文本文件中(可以说没有任何加密)。
--timeout
参数的最大值是多少?
您可以编辑 ~/.gitconfig
文件以存储您的凭据:
sudo nano ~/.gitconfig
哪个应该已经有了
[user]
email = your@email.com
user = gitUSER
您应该在此文件的底部添加以下内容。
[credential]
helper = store
我推荐这个选项的原因是因为它是全局的,如果在任何时候你需要删除你知道去哪里改变它的选项。
仅在您的个人计算机中使用此选项。
然后当你拉 |克隆|输入你的Git密码,一般情况下,密码会以格式保存在~/.git-credentials
中
https://gituser:gitpassword@domain.xxx
其中 DOMAIN.XXX 可能是 github.com、bitbucket.org 或其他
重启你的终端。
exec bash
在浏览了数十篇 Stack Overflow 帖子、博客等之后,我尝试了每一种方法,这就是我想出的。它涵盖了一切。
原版 DevOps Git 凭证和私有包备忘单
这些都是您可以安全地验证 Git 以克隆存储库而无需交互式密码提示的所有方法和工具。
SSH 公钥 SSH_ASKPASS
SSH_ASKPASS
API 访问令牌 GIT_ASKPASS .gitconfig 而不是 .gitconfig [credential] .git-credentials .netrc
GIT_ASKPASS
.gitconfig 而不是
.gitconfig [凭证]
.git 凭证
.netrc
私人套餐(免费)
Node.js / npm package.json
Python/pip/鸡蛋 requirements.txt
红宝石 Gemfile
去吧.mod
银弹
想要Just Works™?这是神奇的银弹。
获取您的访问令牌(如果您需要 GitHub 或 Gitea 说明,请参阅备忘单中的部分)并将其设置在环境变量中(用于本地开发和部署):
MY_GIT_TOKEN=xxxxxxxxxxxxxxxx
对于 GitHub,请逐字复制并运行这些行:
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:"
恭喜。现在,任何克隆 Git 存储库的自动化工具都不会受到密码提示的阻碍,无论是使用 HTTPS 还是使用任何一种 SSH URL 样式。
不使用 GitHub?
对于其他平台(Gitea、GitHub 和 Bitbucket),只需更改 URL。不要更改用户名(尽管是任意的,但不同的配置条目需要它们)。
兼容性
这适用于 macOS、Linux、Windows(在 Bash 中)、Docker、CircleCI、Heroku、Akkeris 等。
更多信息
请参阅备忘单的“.gitconfig insteadOf”部分。
安全
请参阅备忘单的“安全”部分。
git config --global credential."https://somegithost.com".username MyUserName
,它在您的备忘单in 中,但在此答案线程中的其他任何地方都没有。该特定解决方案没有回答 OP 的问题,但它回答了我的问题,所以谢谢!
我认为缓存凭据更安全,而不是永远存储它:
git config --global credential.helper 'cache --timeout=10800'
现在您可以输入您的用户名和密码(git pull
或 ...),并在接下来的三个小时内继续使用 Git。
它既好又安全。
超时的单位是秒(本例中为三小时)。
'cache'
部分被解释为要设置的 credentials.helper
的值,而 --timeout
被解释为 git config
的另一个选项,这会导致语法错误。老实说,考虑到通常如何解析命令选项,这是有道理的。在引号中使用 'cache --timeout=<secs>'
可以明确表明,整个事情必须是 credential.helper
下的设置值,并且更便于移植。没有解释如何阅读 --timemout
部分的空间。
git config --global -e
,正如 Walter white 所说,“你说得对!” :)。谢谢,我会编辑我的答案
只需将您的凭据放在 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
git@github.com:Username/myRepo.git
https
而不是 ssh
克隆 Wiki,因此这样的解决方案在那里可能很有用。另请注意,您可以使用 oauth-token,它比您的 GitHub 密码更安全。
对于全局设置,打开终端(从任何地方),运行以下命令:
git config --global user.name "your username"
git config --global user.password "your password"
这样,您机器上的任何本地 Git 存储库都将使用该信息。
您可以通过执行以下操作单独配置每个存储库:
在存储库文件夹中打开终端。
运行以下命令: git config user.name "你的用户名" git config user.password "你的密码"
它仅影响该文件夹(因为您的配置是本地的)。
--local
标签,但又没有任何反应
git config --global
仍然只能由用户在 ~/.gitconfig
访问,因此从这个意义上说,它可能并不比您的主目录中的私有 ssh 密钥(没有密码保护)或类似 ~/.aws/config
的 aws cli 更糟糕.如果您将 --system
用于 git config,它将可供您计算机上的所有用户使用,因此最好不要这样做,也不需要这样做,尽管它仍然仅限于您计算机上的其他用户。
您可以使用 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。
除了编辑 ~/.gitconfig
文件之外,您还可以从命令行调用:
git config --local --edit
或者
git config --global --edit
https://i.stack.imgur.com/T4Iid.png
也可以使用命令行直接编辑 git config 文件(不用编辑器)
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
表示为项目或操作系统用户保存配置参数。
将用户名和密码存储在 .git-credentials 中
.git-credentials
是您运行 git config --global credential.helper store
时存储您的用户名和密码(访问令牌)的位置,这是其他答案所建议的,然后输入您的用户名和密码或访问令牌:
https://${username_or_access_token}:${password_or_access_token}@github.com
因此,为了保存用户名和密码(访问令牌):
git config —-global credential.helper store
echo “https://${username}:${password_or_access_token}@github.com“ > ~/.git-credentials
这对于 GitHub 机器人非常有用,例如通过为不同的分支设置规则来解决 Chain automated builds in the same Docker Hub repository,然后通过在 Docker Hub 的 post_push
挂钩中推送它来触发它。
可以在 Stack Overflow 上看到 here 的一个示例。
如果您使用 SSH 身份验证比用户名/密码身份验证更安全。
如果您使用的是 Mac,则 SSH 客户端身份验证已集成到 macOS 钥匙串中。创建 SSH 密钥后,在终端中输入:
ssh-add -K ~/.ssh/id_rsa
这会将 SSH 私钥添加到 macOS 钥匙串中。 Git 客户端在连接到远程服务器时将使用 SSH。只要您在服务器上注册了您的 ssh 公钥,就可以了。
SSH
访问权限,您再安全不过了。使用 HTTP 身份验证,窃取凭据的人只能访问 GitHub/GitLab
。令牌也被设计为具有有限的寿命。
在这种情况下,您需要 git credential helper 来告诉 Git 使用以下命令行记住您的 GitHub 密码和用户名:
git config --global credential.helper wincred
如果您正在使用使用 SSH 密钥的存储库,那么您需要 SSH 密钥进行身份验证。
wincred
已过时,请参阅 stackoverflow.com/a/53184233/5281824
以前的答案都不适合我。每次我想fetch
或pull
时,我都会收到以下信息:
Enter passphrase for key '/Users/myusername/.ssh/id_rsa':
对于 Mac
我能够通过以下方式阻止它询问我的密码:
运行以下命令打开配置: vi ~/.ssh/config 添加以下内容: UseKeychain yes 保存并退出:按 Esc,然后输入 :wq!
对于 Windows
我能够使用此 Stack Exchange 帖子中的信息使其工作:How to avoid being asked passphrase each time I push to Bitbucket
只需使用
git config --global credential.helper store
并执行 git pull。它将要求输入用户名和密码。从现在开始,它不会提供任何输入用户名和密码的提示。它将存储详细信息。
截至 2021 年,HTTPS 远程有一个安全的、用户友好的跨平台解决方案。无需再输入密码!不再有 SSH 密钥!不再有个人访问令牌!
安装由 GitHub (downloads) 开发的Git Credential Manager。它支持对 GitHub、BitBucket、Azure 和 GitLab 的无密码浏览器内 OAuth 身份验证。这意味着您可以启用 two-factor authentication on GitHub 和其他平台,从而大大提高您帐户的安全性。
推送时,您可以选择身份验证方法:
> git push
Select an authentication method for 'https://github.com/':
1. Web browser (default)
2. Device code
3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...
在 Linux 上,需要进行少量设置。以下将凭证在内存中缓存 20 小时,因此您每天最多必须进行一次身份验证。
git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"
熟悉 gnome-keyring 或 KWallet 的高级用户可能更喜欢 change the credential store 而不是 libsecret。
化妆品配置 (docs):
更喜欢在终端而不是在 GUI 中选择身份验证方法(点击次数较少)始终使用浏览器方法而不是每次都被询问(甚至更少的按键)
git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser
全局保存用户名和密码:
git config --global user.name "fname lname"
git config --global user.email "example@gmail.com"
git config --global user.password "secret"
获取特定设置,
git config --global --get user.name
git config --global --get user.email
git config --global --get user.password
获取所有 Git 设置:
git config --list --show-origin
查看官方 Git 文档:
如果您使用 SSH 传输连接到遥控器,您可能拥有一个没有密码的密钥,这样您就可以安全地传输数据而无需输入用户名和密码。然而,这对于 HTTP 协议是不可能的——每个连接都需要一个用户名和密码。对于具有双因素身份验证的系统来说,这变得更加困难,因为您用于密码的令牌是随机生成的并且无法发音。幸运的是,Git 有一个凭证系统可以帮助解决这个问题。 Git 在框中提供了一些选项: 默认是根本不缓存。每个连接都会提示您输入用户名和密码。 “缓存”模式将凭据保存在内存中一段时间。没有任何密码存储在磁盘上,并且在 15 分钟后从缓存中清除。 “存储”模式将凭据保存到磁盘上的纯文本文件中,并且它们永不过期。这意味着在您更改 Git 主机的密码之前,您无需再次输入您的凭据。这种方法的缺点是您的密码以明文形式存储在主目录中的纯文本文件中。如果您使用的是 Mac,Git 带有“osxkeychain”模式,该模式将凭据缓存在附加到您的系统帐户的安全钥匙串中。此方法将凭据存储在磁盘上,并且它们永不过期,但它们使用存储 HTTPS 证书和 Safari 自动填充的同一系统进行加密。如果您使用的是 Windows,则可以安装一个名为“Git Credential Manager for Windows”的帮助程序。这类似于上面描述的“osxkeychain”帮助程序,但使用 Windows Credential Store 来控制敏感信息。它可以在 https://github.com/Microsoft/Git-Credential-Manager-for-Windows 找到。您可以通过设置 Git 配置值来选择其中一种方法:
git config --global credential.helper cache
git config --global credential.helper store
来自 7.14 Git Tools - Credential Storage
从 comment by rofrol,在 Linux Ubuntu 上,从 this answer,以下是如何在 Ubuntu 上执行此操作:
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”,因此您可以免费获得它。 OS X 内置版本和自制版本都默认存在。
如果您在 Windows 上使用 Git 凭据管理器...
git config -l
应显示:
credential.helper=manager
但是,如果没有提示您输入凭据,请按照以下步骤操作:
从开始菜单打开控制面板选择用户帐户在左侧菜单中选择管理您的凭据删除与 Git 或 GitHub 相关的任何凭据
如果您有代理并且您的 Git 服务器位于内部网络上,请确保您不设置了 HTTP_PROXY
、HTTPS_PROXY
、NO_PROXY
环境变量。
您还可以使用 git-gui
测试 Git fetch/push/pull,它链接到 C:\Users\<username>\AppData\Local\Programs\Git\mingw64\libexec\git-core
中的凭证管理器二进制文件
对于 Windows 用户,请查看 .gitconfig 文件并检查已为凭据帮助程序配置的内容。如果您有以下...
[凭据“helperselector”] 选择 = wincred
您将在 Windows Credential Manager 中找到凭据。
https://i.stack.imgur.com/RrIIT.png
在那里您可以编辑凭证。
注意:Wincred 已被弃用,请参阅...
因此,或者您可能希望重新配置 Git 以使用内置的 Git 凭证管理器......
git config --global credential.helper manager
在完整阅读答案并尝试了这个问题的大部分答案后,我最终找到了适合我的程序。我想分享它,以防有人必须处理复杂的用例,但仍然不想像我一样浏览所有答案和 gitcredentials、gitcredentials-store 等手册页。
在下面找到我建议如果您(像我一样)必须使用几种不同的用户名/密码组合处理来自多个提供商(GitLab、GitHub、Bitbucket 等)的多个存储库的过程。如果您只有一个帐户可以使用,那么您最好使用 git config --global credential.helper store
或 git config --global user.name "your username"
等解决方案,这些解决方案在之前的答案中已经得到了很好的解释。
我的解决方案:
取消设置全局凭证助手,以防一些以前的实验妨碍 :) git config --global --unset credentials.helper 移动到你的仓库的根目录并禁用本地凭证助手(如果需要) cd /path/to /my/repo git config --unset credential.helper` 创建一个文件来将你的 repo 的凭证存储到 git config credential.helper 'store --file ~/.git_repo_credentials' 注意:这个命令会创建一个名为“.git_repo_credentials”的新文件进入您的主目录,Git 将您的凭据存储到该目录中。如果不指定文件名,Git 使用默认的“.git_credentials”。在这种情况下,只需发出以下命令即可: git config credential.helper store 设置您的用户名 git config credential.*.username my_user_name 注意:如果您的存储库来自同一提供商(例如 GitLab),通常可以使用“*”。相反,如果您的存储库由不同的提供商托管,那么我建议为每个存储库显式设置指向提供商的链接,如下面的示例(对于 GitLab): git config credential.https://gitlab.com.username my_user_name
此时,如果您发出需要您的凭据的命令(例如 git pull
),您将被要求输入与“my_user_name”对应的密码。这只需要一次,因为 git 将凭据存储到“.git_repo_credentials”并在后续访问时自动使用相同的数据。
github...
替换了 https://github...
)
如果 git 客户端不关心安全性,请以这种方式编辑 url:
git remote set-url origin https://${access_token}@github.com/${someone}/${somerepo}.git
在 git clone
情况下相同:
git clone https://${access_token}@github.com/${someone}/${somerepo}.git
我个人不赞成使用 global
域的 git config
,因为这在多帐户情况下会一团糟。
GitHub 的推荐现在变了,最好的方法也是最简单的。 Details here
安装 Github 官方 cli。例如对于 mac:brew install gh。在终端中输入 gh auth login,然后按照提示进行操作。
要将您的用户名和用户密码保存到 github 帐户中,只需依次运行这些命令。
git config --global user.name "userName"
git config --global user.email "usernam@gmail.com"
git config --global user.password "userPassword"
git config --global credential.helper store
git config --list --show-origin
然后使用以下命令生成密钥:
ssh-keygen -t rsa -C "useremail@gmail.com"
注意:复制创建 id_rsa 文件的文件位置然后转到该文件位置 --> 打开 gitbash 或命令提示符 -->运行命令 - cat id_rsa.pub
将显示 SSH 密钥,复制此 SSH 密钥并将其粘贴到您的 gihub/gitlab 帐户中
对于 Windows 用户,这种方式可以工作:
注意:如果您为 GitHub 启用了 two-factor authentication,请将其禁用一段时间
步骤 1 转到控制面板 → 用户帐户 → 凭据管理器 → Windows 凭据
步骤 2 转到通用凭据部分 → 添加通用凭据
第 3 步 - 填写 Internet 或网络地址字段:git.https://github.com 用户名:您的 GitHub 用户名 密码:您的 GitHub 用户名 现在点击 OK。这会将您的 GitHub 帐户的密码和用户名保存到您的本地计算机
git config --global user.name "your username"
git config --global user.password "your password"
查看
git config --list
不定期副业成功案例分享
--global
以仅存储一个存储库的密码(在拉取和推送时),而不是存储任何其他存储库的密码(可能位于不同的主机上,具有不同的凭据)可能出于任何原因不想存储)~/.git-credentials
的保护与像~/.ssh/id_rsa
这样的私钥没有什么不同。因此,如果您的私钥上没有密码,那么~/git-credentials
并不比 ssh 密钥差