我知道这样做有点痛苦,但我的个人帐户和工作帐户有多个帐户。我为这些帐户生成令牌,因此它们的访问受到限制,并且我不必使用我的实际密码,因此更安全。我遇到的问题是,似乎没有一个很好的方法来存储多个帐户的这些密码。这适用于 Windows 和 Linux。我发现的“最简单”的方法就是在系统上拥有多个用户帐户,这不太方便。否则,如果您有一个存储库,并且尝试使用 etc 设置用户git config user.name,那么它无论如何都会为该存储库使用错误的密码/帐户。
有没有更简单或更好的方法来解决这个问题?为 git 保存多个密码并且只能选择要用于存储库的帐户并使其正常工作?
小智 12
人们解决此问题的最常见方法是: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
Run Code Online (Sandbox Code Playgroud)
我更喜欢解决这个问题的方法是使用 credential.namespace git config 选项。想法是为不同的帐户创建不同的命名空间,这样它们就不会在系统凭据管理器中发生冲突。
例如(使用 github.com 作为示例 url),您可以决定其中一个帐户是您的主帐户,并在全局 git 配置中进行设置:
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
Run Code Online (Sandbox Code Playgroud)
然后输入要使用不同凭据登录的存储库并在本地进行设置:
git config --global credential.namespace "MainAccount"
git credential approve
url=https://github.com
username=YourMainAccUsername
password=YourMainAccPasswordOrToken
<Enter>
Run Code Online (Sandbox Code Playgroud)
您可以在凭证管理器中验证,您应该有两条新记录:
cd path/to/other/repo
git config credential.namespace "OtherAccount"
git credential approve
url=https://github.com
username=YourOtherAccUsername
password=YourOtherAccPasswordOrToken
<Enter>
Run Code Online (Sandbox Code Playgroud)
您可以在命令提示符中验证哪些凭据将发送到 git 服务器:
MainAccount:https://github.com/
OtherAccount:https://github.com/
Run Code Online (Sandbox Code Playgroud)
        您可以考虑使用凭证助手,例如GitHub最新发布的(2020 年 7 月) GCM Core。
Git Credential Manager Core (GCM Core) 是一个基于 .NET Core 构建的安全 Git 凭据助手,可在 Windows 和 macOS 上运行。
Linux 的支持已计划,但尚未安排。
对于Linux,同时,您可以使用Java GCM。
(见下面最后一节)
在这两种情况下,用法是相同的:您可以缓存给定 URL 的凭据(例如您的令牌)。
Linux的 2020 年 9 月更新(2 个多月后):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 上的凭证存储中查看更多信息
目前,Git Credential Manager Core (GCM Core) 在 Linux 平台上管理的存储凭据有以下三个选项:
- freedesktop.org 特勤局 API
 - GPG/pass 兼容文件
 - 明文文件
 
2021 年 8 月更新:第 135 期现已由Matthew John Cheetham (GitHub 工作人员)关闭:
解决这个问题是因为GCM Core 现在可以在 Linux 发行版上运行。
对于更多问题/错误,或支持更多发行版,请打开新问题。谢谢!
GCM 2.2.0(2023 年 7 月)添加了login、logout和listGitHub 提供商的命令,以及用于在 GitHub 用户帐户之间进行选择的 UI 和 TTY 提示。
|   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           3984 次  |  
        
|   最近记录:  |