Eds*_*zar 886 git credentials git-config
我想在GitExtension中自动使用push和pull ,而不是每次都在提示符中输入我的用户和密码.
那么如何在GIT中保存我的凭据呢?
Nee*_*ika 1629
git config --global credential.helper store
Run Code Online (Sandbox Code Playgroud)
然后
git pull
Run Code Online (Sandbox Code Playgroud)
提供用户名和密码,稍后将记住这些详细信息.凭据存储在磁盘上的文件中,磁盘权限为"只是用户可读/可写",但仍以明文形式显示.
如果你想稍后更改密码
git pull
Run Code Online (Sandbox Code Playgroud)
将失败,因为它失败了,它从.git-credentials文件中删除了有问题的用户+密码,所以现在运行
git pull
Run Code Online (Sandbox Code Playgroud)
提供新密码,它将像以前一样工作.
Far*_*ihi 277
您可以使用git config在git中启用凭据存储.
git config --global credential.helper store
Run Code Online (Sandbox Code Playgroud)
运行此命令时,第一次从远程存储库中提取或推送时,系统会询问您的用户名和密码.
之后,为了与远程存储库进行后续通信,您不必提供用户名和密码.
存储格式是以.git-credentials纯文本格式存储的文件.
此外,您可以使用其他帮助程序git config credential.helper,即内存缓存:
git config credential.helper cache <timeout>
Run Code Online (Sandbox Code Playgroud)
它取一个可选项timeout parameter,确定其守护程序应运行多长时间以及default它的值900 seconds (15 minutes).
警告:如果您使用此方法,您的git帐户密码将以plaintext格式保存global .gitconfig file,例如在linux中/home/[username]/.gitconfig
如果您不希望这样做,请改用ssh key您的帐户.
小智 99
您可以像这样设置用户名和密码:
git config --global user.name "your username"
git config --global user.password "your password"
Run Code Online (Sandbox Code Playgroud)
sim*_*eco 92
在终端中,输入以下内容:
# Set git to use the credential memory cache
git config --global credential.helper cache
Run Code Online (Sandbox Code Playgroud)
默认情况下,Git会将您的密码缓存15分钟.
要更改默认密码缓存超时,请输入以下内容:
# Set the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'
Run Code Online (Sandbox Code Playgroud)
来自GitHub帮助
Ami*_*aei 44
我认为缓存凭证更安全,而不是永远存储:
git config --global credential.helper 'cache --timeout=10800'
Run Code Online (Sandbox Code Playgroud)
现在您可以输入您的用户名和密码(git pull或...),并在接下来的 3 小时内继续使用 git。
好又安全。
超时以秒为单位(示例中为 3 小时)。
T04*_*435 43
您可以编辑~/.gitconfig到文件存储您的凭据
sudo nano ~/.gitconfig
Run Code Online (Sandbox Code Playgroud)
应该已经有了
[user]
email = your@email.com
user = gitUSER
Run Code Online (Sandbox Code Playgroud)
您应该在此文件的底部添加.
[credential]
helper = store
Run Code Online (Sandbox Code Playgroud)
我建议使用此选项的原因是它是全局的,如果在任何时候您需要删除该选项,您知道去哪里并进行更改.
仅在您的个人计算机中使用此选项.
然后当你拉| 克隆| 输入你的密码,一般来说,密码将以~/.git-credentials格式保存
https://GITUSER:GITPASSWORD@DOMAIN.XXX
Run Code Online (Sandbox Code Playgroud)
在哪里DOMAIN.XXX可能是GITHUB.COM | BITBUCKET.ORG | 其他
见文档
Col*_*nic 28
截至 2021 年,HTTPS 遥控器将有一个安全、用户友好的跨平台解决方案。不再需要输入密码!不再需要 SSH 密钥!不再需要个人访问令牌!
安装GitHub 开发的Git Credential Manager (下载)。它支持对 GitHub、BitBucket、Azure 和 GitLab 进行无密码浏览器内 OAuth 身份验证。这意味着您可以在 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...
Run Code Online (Sandbox Code Playgroud)
在 Linux 上,需要进行一些设置。以下将凭据在内存中缓存 20 小时,因此您每天最多必须进行一次身份验证。
git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"
Run Code Online (Sandbox Code Playgroud)
熟悉 gnome-keyring 或 KWallet 的高级用户可能更喜欢将凭证存储更改为 libsecret。
外观配置(文档):
git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser
Run Code Online (Sandbox Code Playgroud)
Tua*_*wrs 24
对于全局设置,请从任何位置打开终端(运行):
git config --global user.name "your username"
git config --global user.password "your password"
Run Code Online (Sandbox Code Playgroud)
这样,您在计算机上拥有的任何本地git repo都将使用该信息。
您可以通过以下操作为每个存储库分别进行配置:
运行以下命令:
git config user.name "your username"
git config user.password "your password"
Run Code Online (Sandbox Code Playgroud)它仅影响该文件夹(因为您的配置是本地的)。
Nad*_*adu 21
只需将ya凭据放入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
小智 21
要将您的用户名和用户密码保存到 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
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令生成密钥:
ssh-keygen -t rsa -C "useremail@gmail.com"
Run Code Online (Sandbox Code Playgroud)
注意:复制创建 id_rsa 文件的文件位置然后转到该文件位置 --> 打开 gitbash 或命令提示符 --> 运行命令 -cat id_rsa.pub
将显示 SSH 密钥,复制此 SSH 密钥并将其粘贴到您的 gihub/gitlab 帐户中
Dav*_*llo 19
您可以使用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]
Run Code Online (Sandbox Code Playgroud)
您可以检查存储在文件中的凭据 ~/.git-credentials
有关更多信息,请访问 git-credential-store - 帮助程序以在磁盘上存储凭据
Bir*_*Efe 16
如果使用SSH身份验证而不是用户名/密码身份验证,则会更安全.
如果您使用的是Mac,则SSH客户端身份验证将集成到MacOS钥匙串中.创建SSH密钥后,键入终端:
ssh-add -K ~/.ssh/id_rsa
Run Code Online (Sandbox Code Playgroud)
这会将SSH私钥添加到MacOS钥匙串中.git客户端在连接到远程服务器时将使用ssh.只要您在服务器上注册了ssh公钥,就可以了.
小智 12
在这种情况下,您需要使用git凭证帮助程序通过以下命令行告诉git记住您的GitHub密码和用户名:
git config --global credential.helper wincred
Run Code Online (Sandbox Code Playgroud)
如果您使用SSH密钥使用repo,则需要使用SSH密钥进行身份验证.
mri*_*dra 12
全局保存用户名和密码:
git config --global user.name "fname lname"
git config --global user.email "example@gmail.com"
git config --global user.password "secret"
Run Code Online (Sandbox Code Playgroud)
获取特定设置,
git config --global --get user.name
git config --global --get user.email
git config --global --get user.password
Run Code Online (Sandbox Code Playgroud)
获取所有 Git 设置:
git config --list --show-origin
Run Code Online (Sandbox Code Playgroud)
aGu*_*egu 12
如果 git 客户端不关心安全性,请按以下方式编辑 url:
git remote set-url origin https://${access_token}@github.com/${someone}/${somerepo}.git
Run Code Online (Sandbox Code Playgroud)
同样的git clone情况:
git clone https://${access_token}@github.com/${someone}/${somerepo}.git
Run Code Online (Sandbox Code Playgroud)
我个人不喜欢git config域名global,因为在多帐户情况下这会很混乱。
access_token是您可以在 中生成的内容Settings / Developer settings / Personal access tokens。请记住授予它repo范围。
Jia*_* Xu 11
.git-credentials是您运行时存储用户名和密码(访问令牌)的位置git config --global credential.helper store,这是其他答案所建议的,然后输入您的用户名和密码或访问令牌:
https://${username_or_access_token}:${password_or_access_token}@github.com
Run Code Online (Sandbox Code Playgroud)
因此,为了保存用户名和密码(访问令牌):
git config —-global credential.helper store
echo “https://${username}:${password_or_access_token}@github.com“ > ~/.git-credentials
Run Code Online (Sandbox Code Playgroud)
这对于 github 机器人非常有用,例如通过为不同分支设置规则来解决同一 docker 存储库中的 Chain 自动构建,然后通过post_push在 docker hub中的hooker 中推送到它来触发它。
这方面的例子可以看出这里的计算器。
Sal*_*lli 11
在完整阅读答案并尝试了这个问题的大部分答案后,我最终找到了适合我的程序。我想分享它,以防有人必须处理复杂的用例,但仍然不想像我一样浏览所有答案和 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
Run Code Online (Sandbox Code Playgroud)
移至存储库的根目录并禁用本地凭据助手(如果需要)
cd /path/to/my/repo
git config --unset credential.helper`
Run Code Online (Sandbox Code Playgroud)
创建一个文件来存储您的存储库的凭据
git config credential.helper 'store --file ~/.git_repo_credentials'
Run Code Online (Sandbox Code Playgroud)
注意:此命令会在您的主目录中创建一个名为“.git_repo_credentials”的新文件,Git 将您的凭据存储到该文件中。如果不指定文件名,Git 将使用默认的“.git_credentials”。在这种情况下,只需发出以下命令即可:
git config credential.helper store
Run Code Online (Sandbox Code Playgroud)
设置您的用户名
git config credential.*.username my_user_name
Run Code Online (Sandbox Code Playgroud)
注意:如果您的存储库来自同一提供商(例如 GitLab),则使用“*”通常是可以的。如果您的存储库由不同的提供商托管,那么我建议为每个存储库显式设置指向提供商的链接,如以下示例(适用于 GitLab):
git config credential.https://gitlab.com.username my_user_name
Run Code Online (Sandbox Code Playgroud)
此时,如果您发出需要您的凭据的命令(例如git pull),系统将要求您提供与“my_user_name”相对应的密码。这只需要一次,因为 git 将凭证存储到“.git_repo_credentials”并在后续访问中自动使用相同的数据。
pro*_*sti 10
除了编辑~/.gitconfig文件外,如果您询问,您还可以这样做:
git config --local --edit
Run Code Online (Sandbox Code Playgroud)
或者
git config --global --edit
Run Code Online (Sandbox Code Playgroud)
git config --local user.name 'your username'
git config --local user.password 'your password'
Run Code Online (Sandbox Code Playgroud)
或者
git config --global user.name 'your username'
git config --global user.password 'your password'
Run Code Online (Sandbox Code Playgroud)
如果您使用双引号,您的用户名和密码可能会使用一些会破坏密码的字符。
--local或--global表示为项目或操作系统用户保存配置参数。
rog*_*ack 10
从rofrol在 Linux Ubuntu 上的评论,从这个答案,这里是如何在 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
Run Code Online (Sandbox Code Playgroud)
其他一些发行版提供了二进制文件,因此您不必构建它。
在 OS X 中,它通常是“内置”的,并添加了“osxkeychain”的默认模块,因此您可以免费获得它。OS X 内置版本和自制版本都默认存在它。
只是使用
git config --global credential.helper store
Run Code Online (Sandbox Code Playgroud)
并执行git pull,它会询问用户名和密码,从现在开始它不会提供任何用户名和密码的提示,它将存储详细信息
对于 Windows 用户,请查看.gitconfig文件并检查为凭据帮助程序配置的内容。如果您有以下情况...
[凭证“helperselector”] 选定 = wincred
您将在 Windows凭据管理器中找到凭据。
您可以在那里编辑凭据。
注意:Wincred 已被弃用,请参阅...
因此,您可能想要重新配置 Git 以使用内置的 Git 凭证管理器...
git config --global credential.helper manager
Run Code Online (Sandbox Code Playgroud)
上面的答案对我都不起作用。每当我想要fetch或以下时,我都会不断获取以下内容pull:
Enter passphrase for key '/Users/myusername/.ssh/id_rsa':
对于Mac
我可以通过以下方式阻止我询问密码:
vi ~/.ssh/configUseKeychain yes:wq!对于Windows
我能够使用此stackexchange中的信息使其工作:https : //unix.stackexchange.com/a/12201/348665
查看官方 git 文档:
如果您使用 SSH 传输连接到远程设备,您可能会拥有一个没有密码短语的密钥,这样您就可以在不输入用户名和密码的情况下安全地传输数据。但是,这对于 HTTP 协议是不可能的——每个连接都需要用户名和密码。对于具有双因素身份验证的系统来说,这变得更加困难,其中您用于密码的令牌是随机生成且不可发音的。
幸运的是,Git 有一个凭证系统可以帮助解决这个问题。Git 在框中提供了一些选项:
默认是根本不缓存。每次连接都会提示您输入用户名和密码。
“缓存”模式将凭据保存在内存中一段时间。没有任何密码存储在磁盘上,它们会在 15 分钟后从缓存中清除。
“存储”模式将凭据保存到磁盘上的纯文本文件中,并且它们永不过期。这意味着在您更改 Git 主机的密码之前,您将永远不必再次输入您的凭据。这种方法的缺点是您的密码以明文形式存储在主目录中的纯文件中。
如果您使用的是 Mac,Git 带有“osxkeychain”模式,它将凭据缓存在附加到您的系统帐户的安全钥匙串中。此方法将凭据存储在磁盘上,并且它们永不过期,但它们使用存储 HTTPS 证书和 Safari 自动填充的相同系统进行加密。
如果您使用的是 Windows,您可以安装一个名为“Git Credential Manager for Windows”的帮助程序。这类似于上面描述的“osxkeychain”帮助程序,但使用 Windows 凭据存储来控制敏感信息。它可以在https://github.com/Microsoft/Git-Credential-Manager-for-Windows上找到 。
您可以通过设置 Git 配置值来选择以下方法之一:
$ git config --global credential.helper cache
$ git config --global credential.helper store
Run Code Online (Sandbox Code Playgroud)
https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage
如果您在 Windows上使用Git Credential Manager ...
git config -l 应该显示:
credential.helper=manager
Run Code Online (Sandbox Code Playgroud)
但是,如果没有提示您输入凭据,请执行以下步骤:
如果您有代理并且您的 Git 服务器在内部网络上,还要确保您没有设置HTTP_PROXY, HTTPS_PROXY,NO_PROXY环境变量。
您还可以使用git-gui其中的凭据管理器二进制文件的链接来测试 Git fetch/push/pullC:\Users\<username>\AppData\Local\Programs\Git\mingw64\libexec\git-core
在浏览了数十篇 SO帖子,博客等之后,我尝试了每种方法,这就是我想出的方法。它涵盖了一切。
这些都是您可以安全地验证git来克隆存储库的所有方式和工具,而无需交互式密码提示。
想要Just Works™吗?这是神奇的银弹。
获取您的访问令牌(如果需要Github或Gitea指令,请参阅备忘单中的部分),并将其设置在环境变量中(本地开发和部署都需要):
MY_GIT_TOKEN=xxxxxxxxxxxxxxxx
Run Code Online (Sandbox Code Playgroud)
对于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:"
Run Code Online (Sandbox Code Playgroud)
恭喜,现在无论使用https还是ssh url样式,任何自动克隆git存储库的工具都不会受到密码提示的阻碍。
不使用Github?
对于其他平台(Gitea,Github,Bitbucket),只需更改URL。请勿更改用户名(尽管任意,但对于不同的配置条目来说是必需的)。
兼容性
这可以在MacOS,Linux,Windows(在Bash中),Docker,CircleCI,Heroku,Akkeris等本地运行。
更多信息
请参阅备忘单的“ .gitconfig代替Of”部分。
安全
请参阅备忘单的“安全性”部分。
GitHub 的建议现在已经改变,最好的方法也是最简单的。详细信息在这里
brew install gh.gh auth login在终端中输入内容,然后按照提示操作。| 归档时间: |
|
| 查看次数: |
776282 次 |
| 最近记录: |