如何在不同的注册用户下克隆 git 私人仓库?

xia*_*dai 7 git github

我有一个已经使用多年的 GitHub 用户名,但是,在加入新公司后,他们要求我在公司电子邮件地址下注册一个新的 GitHub 帐户。现在我无法克隆私有存储库,因为 user.name 被设置为我的普通 GitHub 句柄。

在此页面上看到,我可以在 URL 之前添加我的用户名,例如git clone https://usernamenew@github.com/companyname/repo-name.git.

但是,我收到错误

Cloning into 'repo-name'...
remote: Repository not found.
fatal: repository 'https://usernamenew@github.com/companyname/repo-name.git/' not found
Run Code Online (Sandbox Code Playgroud)

如何使用不同的用户名克隆公司的存储库?

Rob*_*rek 4

根据您的示例输出,git 不会要求您输入密码。看起来像:

  • 您的凭据已缓存
  • 这些凭据适用于您的个人帐户,而不是您的新公司帐户

您已将新用户名正确添加到 URL 中。但默认情况下,git 在应用缓存凭据时只关心主机。

您也可以使用useHttpPath选项gitcredentials来告诉 git 考虑 URL 路径。这将允许您对不同的存储库使用不同的帐户。

https://git-scm.com/docs/gitcredentials.html#Documentation/gitcredentials.txt-useHttpPath

使用HttpPath

默认情况下,Git 不认为 http URL 的“路径”部分值得通过外部帮助程序进行匹配。这意味着为https://example.com/foo.git存储的凭证也将用于https://example.com/bar.git。如果您确实想区分这些情况,请将此选项设置为 true。

通过运行以下命令进行配置:

git config --global credential.https://github.com.useHttpPath true
Run Code Online (Sandbox Code Playgroud)