当我尝试使用ssh推送时,Git尝试使用全局用户

时间:2019-04-13 12:41:51

标签: git ssh

我的全局用户覆盖我的本地git用户。

这里是完整的上下文: 我有2个Github帐户,所以我想为这些存储库设置ssh密钥。

我已经尝试了所有这些指南。

article 1

article 2

article 3

article 4

这是我的~/.ssh/config

# Personal GitHub
Host github.com
   HostName github.com
   User mirkancal
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa
   IdentitiesOnly yes
# Yazılım Kafe
Host github.com-yazilimkafe
   HostName github.com
   User yazilimkafe
   PreferredAuthentications publickey
   IdentityFile ~/.ssh/id_rsa_yazilimkafe
   IdentitiesOnly yes

我的git remote -v输出(用于yazilimkafe回购)

origin  git@github.com-yazilimkafe:yazilimkafe/git-dersi.git (fetch)
origin  git@github.com-yazilimkafe:yazilimkafe/git-dersi.git (push)

我已相应添加了本地用户名和电子邮件。 相关SO post 1 SO post 2

这是我的本地gitconfig

[user]
        name = yazilimkafe
        email = mirkancaliskan.dev@gmail.com

所以我相信我已经准备好了,但是当我尝试使用git push origin master

它给了我:

ERROR: Permission to yazilimkafe/git-dersi.git denied to mirkancal.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我认为在ssh方面,我没有问题,但是我不能阻止我的全局用户不在仓库中覆盖我的本地用户。我的问题是,该怎么做?我在ssh部分做错了吗?

1 个答案:

答案 0 :(得分:1)

针对您的具体情况,针对此处Push to .git but denied to wrong user , finally solved发布的解决方案进行了扩展,我转载了您的情况,这对我很有用。

设置两个GitHub主机名

第1步:使用ssh-add加载所有身份。

第2步。将这些条目添加到.ssh/config

Host github-yazilimkafe
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_yazilimkafe

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

第3步。克隆存储库或使用这些名称设置远程URL

git clone github-yazilimkafe:yazilimkafe/git-dersi.git
git clone github-mirkancal:mirkancal/some-project.git

这时,您应该能够轻松地在任一沙箱中进行推送和获取,并且无需在两者之间重置代理。当您在该沙箱中通过ssh登录时,主机名将使您的代理为github提供正确的身份。

也使用名称github.com

如果您还想使用主机名github.com,即直接克隆git@github.com/...,则必须担心ssh代理显示ssh密钥的顺序。使用ssh-add添加身份的顺序似乎无关紧要。就我而言,.ssh/id_rsa是我的代理提供的默认密钥,但并非总是如此。

如果您使用的是gnome-keyring,那么在此问题中使用单独文件夹的建议似乎会有所帮助:https://unix.stackexchange.com/questions/429730/how-to-change-the-order-of-keys-in-gnome-keyring

但是,使用ssh代理的Cygwin / OpenSSH安装,我无法将.ssh/id_rsa_2设置为默认值。

因此,在我的情况下,我必须在.ssh/config中使用命名条目才能将.ssh/id_rsa_2与GitHub一起使用,但是在需要github.com时可以使用名称.ssh/id_rsa ,我的主键。

相关问题