Github mac git 使用多个帐户

问题描述 投票:0回答:3

我在 Github 上有两个帐户,每个帐户都有特定的用途。

我想确保每次我推送我的姓名和密码时,我都可以使用正确的帐户进行大声操作。

但这并没有发生,我使用 https 连接而不是 ssh。

现在查看钥匙链,我看到找到了两把钥匙。 userA 一把钥匙,userB 一把钥匙,均适用于 Github。

你能帮我一下吗?

git macos github version-control keychain
3个回答
5
投票

按照 https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token

中所述为自己获取个人访问令牌

一旦您拥有个人访问令牌,您就可以将其用作您帐户的密码。所以每个帐户都会获得一个。

要使用您的个人访问令牌,请添加您的 github 帐户名,并使用

username:passowrd@
语法将该令牌用作 http URL 的密码。例如,如果您想克隆我的 httpstress 存储库,您可以这样做:

git clone https://YOUR_GITUB_ACCOUNT:[email protected]/slebetman/httpstress.git

如果您已经克隆了存储库,则可以编辑

.git/config
文件并编辑 github URL 以包含您的帐户名和个人访问令牌。在您的
.git/config
文件中找到以下部分:

[remote "origin"]
    url = https://github.com/slebetman/httpstress.git
    fetch = +refs/heads/*:refs/remotes/origin/*

并将其更改如下:

[remote "origin"]
    url = https://YOUR_GITUB_ACCOUNT:[email protected]/slebetman/httpstress.git
    fetch = +refs/heads/*:refs/remotes/origin/*

0
投票

就我而言,我使用了 2 个 Github 帐户。
并且

git-credential
尝试使用错误的密码/令牌。

解决方案#1:

  • 使用以下命令将 git 配置为使用凭据的完整路径:
git config --global credential.usehttppath true

然后,您可以在全局 git 配置文件中看到更新

~./gitconfig

[credential]
    usehttppath = true
  • 现在,当您尝试
    git clone/fetch/...
    时,每个存储库都会使用
    Username
    Password
  • 现在,将使用右侧
    Password

解决方案#2:

  • 确保 git repo 远程 URL(在
    repo/.git/config
    文件内)包含您的
    Username
    。 所以代替:
[remote "origin"]
        url = https://github.com/company-x/project-y.git

您可以使用以下表格:

[remote "origin"]
        url = https://{Username}@github.com/company-x/project-y.git
  • 现在,将使用右侧
    Password

-1
投票

此信息记录在 Git 常见问题解答中。有关于如何使用 HTTPS 执行此操作如何使用 SSH 执行此操作的说明。

如果您有最新版本的 Git,您可以通过运行

man gitfaq
来阅读常见问题解答。

© www.soinside.com 2019 - 2024. All rights reserved.