git config user.name
等设置用户,那么它将在该仓库中错误地使用错误的密码/账户。有没有更简单或更好的方法来解决这个问题?是否可以保存多个用于git的密码,只需选择要用于仓库的账户即可正常工作?
git config user.name
等设置用户,那么它将在该仓库中错误地使用错误的密码/账户。大多数人解决这个问题的常见方法是: git config --global credential.useHttpPath true
- 并对每个仓库在凭据存储中记录不同的用户名和密码。但是,如果您有大量仓库,这可能会很快污染凭据存储,使其并不是一个非常好的解决方案。
你最终会得到类似如下的记录:
git:https://PersonalAccessToken@github.com/YourUsername/YourRepo1.git
git:https://PersonalAccessToken@github.com/YourUsername/YourRepo2.git
git:https://PersonalAccessToken@github.com/YourUsername/YourRepo3.git
git:https://github.com/YourUsername/YourRepo1.git
git:https://github.com/YourUsername/YourRepo2.git
git:https://github.com/YourUsername/YourRepo3.git
我解决这个问题的方式是使用 credential.namespace git 配置选项。思路是为不同的账户创建不同的命名空间,以便它们不会在系统凭据管理器中发生冲突。
例如(以 github.com 为例),您可以决定其中一个帐户是主要帐户,并在全局 git 配置中设置它:
git config --global credential.namespace "MainAccount"
git credential approve
url=https://github.com
username=YourMainAccUsername
password=YourMainAccPasswordOrToken
<Enter>
然后进入您想要使用不同凭据登录的存储库,并在本地设置:
cd path/to/other/repo
git config credential.namespace "OtherAccount"
git credential approve
url=https://github.com
username=YourOtherAccUsername
password=YourOtherAccPasswordOrToken
<Enter>
您可以在凭据管理器中验证,您应该有两个新记录:
MainAccount:https://github.com/
OtherAccount:https://github.com/
您可以在命令提示符中验证将发送到Git服务器的凭据:
git credential fill
url=https://github.com
<Enter>
Git Credential Manager Core(GCM Core)是一个基于.NET Core构建的安全Git凭据助手,可在Windows和macOS上运行。
Linux的支持计划中,但尚未安排。
对于Linux,在此期间,您可以使用Java GCM。
(请参阅下面的最后一节)
在两种情况下,使用方式都相同:您可以为给定的URL缓存凭据(如令牌)。
2020年9月更新(2个多月后)对于 Linux :{{link1:microsoft/Git-Credential-Manager-Core
问题135}}提到,来自GitHubber Matthew John Cheetham:
我们有一个支持Linux的GCM Core预发布版本!
目前我们提供一个Debian软件包和一个
git-credential-manager-core
单个二进制文件的tarball。
.deb
目前没有签名,并且除了在GitHub上之外没有上传到任何地方。
我们正在进行软件包签名设置的过程,并将其发布到官方的Microsoft源(这样您就可以使用apt-get
)。
当前,预构建的二进制文件仅提供给64位Intel处理器。
请查看更多信息:Linux 上的凭据存储
目前在 Linux 平台上,Git Credential Manager Core (GCM Core) 管理的凭据存储有以下三种选项:
- freedesktop.org Secret Service API
- GPG/pass 兼容文件
- 纯文本文件
将此问题关闭,因为GCM Core现在可以在Linux发行版上运行。
如有其他问题/错误或需要支持更多发行版,请提出新的问题。谢谢!
GCM 2.2.0(2023年7月)为GitHub提供商增加了login
、logout
和list
命令,以及用于在GitHub用户账户之间进行选择的UI和TTY提示。
user.name
和user.email
),还是如何设置不同的凭据集以推送到远程?这两个问题都有答案,但它们是不同的问题。 - bk2204