如何对不同的 git-service 端点使用不同的 PAT?
我使用 GitLab 和 GitHub 来完成不同类型的工作。它们都有不同的个人访问令牌 (PAT),这些令牌在创建时就会显示。目前,我将它们放入密码管理器中,并根据存储库或操作进行复制/粘贴。这是一个令人沮丧、耗时且容易出错的过程。
有没有办法将 git 配置为基于 HTTPS 端点使用不同的凭据?当我访问 gitlab.com 时(无论是克隆、推送、创建等等),它使用一组凭据,而当我访问 github.com 时,它使用另一组凭据?
我在 MacOs 中使用命令行Windows WSL - 如果这很重要的话,都可以使用 zsh。
I use both GitLab and GitHub(s) for different kinds of work. They all have their different Personal Access Tokens (PATs) that are displayed once at creation. Currently I put them in a password manager and copy/paste per repo or action. A frustrating, time-consuming, and mistake prone process.
Is there a way to configure git to use different credentials based on HTTPS endpoint? When I go to gitlab.com - whether cloning, pushing, creating, whatever - it uses one set of credentials, and when going to github.com it uses another?
I'm using the command line in MacOs & Windows WSL - both with zsh if that is important.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您使用 Git Credential Manager,您可以让它将凭证存储在每个路径的基础。 GCM 可以与 MacOS 的钥匙串和 Windows 凭据管理器一起使用,无需额外配置。对于 Linux,配置取决于您的发行版/工具链。 GCM 至少附带了最新版本的 Windows 版 Git。对于 Mac 和 Linux 我不确定,所以你可能需要单独安装。
您确实需要在 git 配置中添加一些额外的配置,以便在您使用多个凭据访问单个主机上的存储库时发挥作用。
这样,GCM 就知道按路径存储凭据,而不是在主机级别存储凭据。这样,它会根据您配置的每个路径进行提示,并存储凭据。
您还可以在不同的用户中显式解析您的原始 URL。 GCM 将使用它来存储不同的令牌:
更多 GCM 文档中有关支持多个身份的详细信息
您还可以在 WSL 中配置您的 git 以使用 Windows 中安装的 GCM 来存储访问密钥位于一个中心位置。
如果您还想存储不同的名称和电子邮件,那么您可以采用的一个技巧是将所有工作存储库放在一个文件夹中,将所有私人存储库放在本地的另一个文件夹中。然后在该文件夹的根目录中,您可以创建另一个
.gitconfig
并将名称和电子邮件配置放入其中。 Git 在查看全局 git 配置之前会遍历该路径来查找配置信息。If you're using the Git Credential Manager you can have it store the credentials on a per path basis. GCM can be used on with MacOS's keychain and the Windows Credentials Manager without additional configuration. For Linux the configuration depends on your distro/tool chain. GCM ships with recent versions of Git for Windows at least. For Mac and Linux I'm not sure about that, so you may need to install it separately.
You do need to add some extra config to your git config for that to work in case you access repos on a single host with multiple credentials.
That way GCM knows to store the credentials on a per path basis instead of at the host level. That way it prompts per path you configure and will store the credintials.
You can also explicitly parr in a different user to your origin url. GCM will use this to store a different token:
More details in the GCM docs on supporting multiple identities
You can also configure your git in WSL to use the GCM installed in windows to store the access keys in one central place.
If you also want to store a different name and email, then one trick you can employ is, to put all your work repos in one folder and all your private repos in another locally. Then in the root of that folder, you can create another
.gitconfig
and put the name and email configs in there. Git will traverse the path to look for configuration information prior to looking at the global git config.