Github mac git 使用多个帐户

Pau*_*aul 3 git macos version-control github keychain

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

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

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

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

你能帮我个忙吗?

sle*_*man 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:YOUR_ACCESS_TOKEN@github.com/slebetman/httpstress.git
Run Code Online (Sandbox Code Playgroud)

如果您已经克隆了存储库,则可以编辑.git/config文件并编辑 github URL 以包含您的帐户名和个人访问令牌。在您的文件中找到以下部分.git/config

[remote "origin"]
    url = https://github.com/slebetman/httpstress.git
    fetch = +refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)

并将其更改如下:

[remote "origin"]
    url = https://YOUR_GITUB_ACCOUNT:YOUR_ACCESS_TOKEN@github.com/slebetman/httpstress.git
    fetch = +refs/heads/*:refs/remotes/origin/*
Run Code Online (Sandbox Code Playgroud)