使用 HTTPS 和 personal access token 参与多个帐户
或者,如果要为这两个帐户都使用 HTTPS 协议,可以配置 Git 以便为每个存储库存储不同的凭据,从而为每个帐户使用不同的 personal access token。
-
打开终端。
-
若要确认凭据管理器的使用,请输入以下命令并记下输出。
Shell git config --get credential.helper
git config --get credential.helper
-
如果输出确认在使用凭据管理器,请为凭据管理器清除存储的凭据。
- 如果输出不包含凭据管理器的名称,则未配置凭据管理器,可以继续执行下一步。
-
如果输出为
osxkeychain
,则在使用 macOS 密钥链。 若要清除凭据,可在命令行上使用凭据帮助程序:$ git credential-osxkeychain erase host=github.com protocol=https > [Press Return] >
-
如果输出为
manager
(在旧版本中为manager-core
),则你使用的是 Git 凭据管理器。 若要清除凭据,请运行以下命令。Shell echo "protocol=https\nhost=github.com" | git credential-manager erase
echo "protocol=https\nhost=github.com" | git credential-manager erase
-
-
若要将 Git 配置为缓存你在 GitHub 上访问的每个存储库的完整远程 URL 的凭据,请输入以下命令。
Shell git config --global credential.https://github.com.useHttpPath true
git config --global credential.https://github.com.useHttpPath true
-
请为每个帐户创建具有
repo
范围的专用 personal access token (classic)。 或者,对于每个帐户以及你所属的每个组织,请创建一个可访问所需存储库并对存储库内容具有读取和写入权限的 fine-grained personal access token。 有关详细信息,请参阅“管理个人访问令牌”。 -
首次使用 Git 克隆存储库或访问已克隆的存储库中的数据时,Git 会请求凭据。 为可访问存储库的帐户提供 personal access token。
Git 将根据存储库的完整远程 URL 缓存 personal access token,你将能够使用正确的帐户在 GitHub.com 上访问和写入存储库数据。
-
打开 Git Bash。
-
若要确认凭据管理器的使用,请输入以下命令并记下输出。
Shell git config --get credential.helper
git config --get credential.helper
-
如果输出确认在使用凭据管理器,请为凭据管理器清除存储的凭据。
-
如果输出不包含凭据管理器的名称,则未配置凭据管理器,可以继续执行下一步。
-
如果输出为
manager
(在旧版本中为manager-core
),则你使用的是 Git 凭据管理器。 若要清除凭据,请运行以下命令。Shell echo "protocol=https`nhost=github.com" | git credential-manager erase
echo "protocol=https`nhost=github.com" | git credential-manager erase
-
如果输出为
wincred
,则在使用 Windows 凭据管理器。 若要清除凭据,请输入以下命令。Shell cmdkey /delete:LegacyGeneric:target=git:https://github.com
cmdkey /delete:LegacyGeneric:target=git:https://github.com
-
-
若要将 Git 配置为缓存你在 GitHub 上访问的每个存储库的完整远程 URL 的凭据,请输入以下命令。
Shell git config --global credential.https://github.com.useHttpPath true
git config --global credential.https://github.com.useHttpPath true
-
请为每个帐户创建具有
repo
范围的专用 personal access token (classic)。 或者,对于每个帐户以及你所属的每个组织,请创建一个可访问所需存储库并对存储库内容具有读取和写入权限的 fine-grained personal access token。 有关详细信息,请参阅“管理个人访问令牌”。 -
首次使用 Git 克隆存储库或访问已克隆的存储库中的数据时,Git 会请求凭据。 为可访问存储库的帐户提供 personal access token。
Git 将根据存储库的完整远程 URL 缓存 personal access token,你将能够使用正确的帐户在 GitHub.com 上访问和写入存储库数据。
-
打开终端。
-
若要确认凭据管理器的使用,请输入以下命令并记下输出。
Shell git config --get credential.helper
git config --get credential.helper
-
如果输出确认在使用凭据管理器,请为凭据管理器清除存储的凭据。
-
如果输出不包含凭据管理器的名称,则未配置凭据管理器,可以继续执行下一步。
-
如果输出为
manager
(在旧版本中为manager-core
),则你使用的是 Git 凭据管理器。 若要清除凭据,请运行以下命令。Shell echo "protocol=https\nhost=github.com" | git credential-manager erase
echo "protocol=https\nhost=github.com" | git credential-manager erase
-
若要将 Git 配置为缓存你在 GitHub 上访问的每个存储库的完整远程 URL 的凭据,请输入以下命令。
Shell git config --global credential.https://github.com.useHttpPath true
git config --global credential.https://github.com.useHttpPath true
-
请为每个帐户创建具有
repo
范围的专用 personal access token (classic)。 或者,对于每个帐户以及你所属的每个组织,请创建一个可访问所需存储库并对存储库内容具有读取和写入权限的 fine-grained personal access token。 有关详细信息,请参阅“管理个人访问令牌”。 -
首次使用 Git 克隆存储库或访问已克隆的存储库中的数据时,Git 会请求凭据。 为可访问存储库的帐户提供 personal access token。
Git 将根据存储库的完整远程 URL 缓存 personal access token,你将能够使用正确的帐户在 GitHub.com 上访问和写入存储库数据。
使用 SSH 和 GIT_SSH_COMMAND
参与多个帐户
如果要为这两个帐户都使用 SSH 协议,则可以为每个帐户使用不同的 SSH 密钥。 有关使用 SSH 的详细信息,请参阅“通过 SSH 连接到 GitHub”。
若要为克隆到工作站的不同存储库使用不同的 SSH 密钥,必须为 Git 操作编写 shell 包装器函数。 该函数应执行以下步骤。
- 使用命令(例如
git config --get remote.origin.url
)确定存储库的全名及所有者。 - 选择用于身份验证的正确 SSH 密钥。
- 相应地修改
GIT_SSH_COMMAND
。 有关GIT_SSH_COMMAND
的详细信息,请参阅 Git 文档中的环境变量。
例如,以下命令设置 GIT_SSH_COMMAND
环境变量以指定 SSH 命令,该命令使用 PATH/TO/KEY/FILE 处的私钥文件进行身份验证,以克隆 GitHub.com 上名为 OWNER/REPOSITORY 的存储库。
GIT_SSH_COMMAND='ssh -i PATH/TO/KEY/FILE -o IdentitiesOnly=yes' git clone git@github.com:OWNER/REPOSITORY
GIT_SSH_COMMAND='ssh -i PATH/TO/KEY/FILE -o IdentitiesOnly=yes' git clone git@github.com:OWNER/REPOSITORY