git 多帐户和存储库问题

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

我想在 Visual Studio 项目中使用 git。我已经有一个运行良好的个人帐户。现在,我的雇主给了我一个新帐户,而 Visual Studio 仍在尝试推送到我的个人帐户,并在发布和同步时给我错误。

所以我遵循了本教程:https://code.tutsplus.com/tutorials/quick-tip-how-to-work-with-github-and-multiple-accounts--net-22574

  • 创建了新的 ssh 密钥
  • 为其创建了身份
  • 将 ssh 密钥添加到我的 github 帐户
  • 向 ssh 配置添加身份

现在有些事情我不清楚

我创建了 2 个原点 + 默认的原点,它们是 原点(默认) myorigin(未使用) georigin(路径:git@github-ge:ge/geweb)

当我打字时 $ git config --global -l 它给了我正确的(雇主的)用户名和用户电子邮件

geweb 是我的新存储库的名称 我的存储库的网址是:https://github.com/ge/geweb.git

当我尝试推送到 georigin 时,它说 未找到存储库 当我尝试推到原点时它说 错误:src refspec master 与任何内容都不匹配。

另外,我没有在任何地方提到我的第二个 git 帐户的用户名和密码

另外,我的 git 帐户 user.email 中有 2 封电子邮件,配置中的电子邮件不是主要的。难道是这个原因吗?

我对它端到端的工作方式感到很困惑。

推送失败后,我尝试从源中解除项目绑定,并从项目文件夹中删除了 .git 文件 但团队资源管理器仍然显示未同步的提交和更改

输出 git Remote -v 是:

  • myorigin git@github-ge:ge:getWeb.git(获取)
  • myorigin git@github-ge:ge:geWeb.git(推送)
git visual-studio github ssh version-control
5个回答
2
投票

假设您的私人帐户和存储库是https://github.com/my/repo.git,您要使用的第二个帐户和存储库是:https://github.com/ge/geweb.git 。整个步骤如下:

  1. ssh-keygen -t rsa -C "second account email address"
  2. 将文件重命名为 id_rsa,例如
    Enter file in which to save the key (/c/Users /.ssh/id_rsa): /c/Users /.ssh/id_rsa_ge
  3. 在目录
    c:\Users \.ssh
    中将
    id-rsa_ge.pub
    的内容复制到第二个github帐户作为SSH密钥。
  4. touch /c/Users /.ssh/config
  5. 修改配置内容如下:

Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa

Host github-ge
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_ge

  1. 通过
    git clone https://github.com/my/repo.git
  2. 克隆您的私人仓库
  3. git remote add geweb git@github-ge:ge/geweb.git
  4. git push geweb master
    git push geweb master -f

1
投票

origin是什么以及git@github-ge在路径git@github-ge:ge/geweb中代表什么

origin
是为远程存储库指定的任意名称。它通常代表本地存储库克隆自身的存储库。

例如,如果您运行

git clone [email protected]:jquery/jquery.git
,那么您最终会得到
origin
指向克隆的 URI。

URI 的

git@github-ge
部分代表服务器上的用户 (
user@server
)。您可以在此处阅读有关 Git 协议(包括 SSH 协议)的信息

对于大多数人来说,URI 的这一部分意味着 Git 使用 SSH 而不是 HTTPS 与远程通信。实际上,SSH 需要 SSH 密钥并运行 SSH 代理,而 HTTPS 需要用户名/密码。


0
投票

所以,事实证明我在设置帐户时遇到了多个问题。

  1. 它选择了错误的来源,肖恩和玛丽娜发现了

所以,我删除了错误的并添加了正确的(georign)

  1. 我已将自述文件添加到默认存储库,但它给了我错误: 提示:更新被拒绝,因为当前分支的尖端落后 提示:它的远程对应物。集成远程更改(例如 提示:'git pull ...'),然后再次推送。 提示:有关详细信息,请参阅“git push --help”中的“有关快进的注意事项”。

所以我在推之前尝试了拉动,结果我必须--allow-unrelated-hitories

git pull georigin master --allow-unrelated-histories

然后

git push georigin master

就是这样!!! (唷)


0
投票

我设法通过从我的文件中删除主机 * 来解决此问题,检查

~/.ssh/config
不起作用

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

Host github.com-workaccount
  AddKeysToAgent yes
  UseKeychain yes
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github.com-workaccount

现在,检查一下运行良好的那个

Host github.com
  AddKeysToAgent yes
  HostName github.com
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

Host github.com-workaccount
  AddKeysToAgent yes
  UseKeychain yes
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_rsa_github.com-workaccount

谢谢


0
投票

我一直在努力解决这个问题,并使用 ssh 密钥尝试了一些复杂的解决方案。不确定这些解决方案是否有优势,但对我来说,我不想克隆我的存储库。

我发现这个解决方案是最简单的,您只需添加第二个遥控器,然后推送到两个遥控器即可:

https://dev.to/hashcode01/add-a-second-remote-origin-to-git-35a7#:~:text=To%20add%20a%20second%20remote,remote%20origins%20for%20the %20存储库.

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