当我尝试使用ssh推送时,Git尝试使用全局用户 [英] Git tries to use global user when I try to push with ssh
问题描述
我的全局用户覆盖我的本地git用户.
My global user overrides my local git user.
这里是完整的上下文: 我有2个Github帐户,所以我想为这些存储库设置ssh密钥.
Here's the full context: I have 2 Github accounts, so I wanted to set my ssh keys for those repos.
我已经尝试了所有这些指南.
I've tried all of those guides.
这是我的~/.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回购
My git remote -v
output for the repo of yazilimkafe
origin git@github.com-yazilimkafe:yazilimkafe/git-dersi.git (fetch)
origin git@github.com-yazilimkafe:yazilimkafe/git-dersi.git (push)
我已经相应地添加了本地用户名和电子邮件. 相关 SO帖子1 SO post 2
I've added local usernames and emails accordingly. Related SO post 1 SO post 2
这是我的本地gitconfig
Here's my local gitconfig
[user]
name = yazilimkafe
email = mirkancaliskan.dev@gmail.com
所以我相信我已经准备好了,但是当我尝试使用git push origin master
So I believe I'm all set but when I try to use 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部分做错了吗?
I think on the ssh side, I don't have a problem, but I can't prevent my global user to not the override my local user in the repo. My question is, how to do that? Am I doing something wrong on the ssh part?
推荐答案
Expanding on the solution posted here Push to .git but denied to wrong user , finally solved for your specific situation, I have reproduced your scenario and this worked for me.
设置两个GitHub主机名
第1步:使用ssh-add
加载所有身份.
Step 1: Load all your identities using ssh-add
.
第2步.将这些条目添加到.ssh/config
Step 2. Add these entries to .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
Step 3. clone the repos or set the remote urls using those names
git clone github-yazilimkafe:yazilimkafe/git-dersi.git
git clone github-mirkancal:mirkancal/some-project.git
这时,您应该能够轻松地在任一沙箱中进行推送和获取,并且无需在两者之间重置代理.当您在该沙箱中通过ssh登录时,主机名将使您的代理为github提供正确的身份.
At this point, you should be able to push and fetch in either sandbox without any difficulty, and without resetting your agent in between. The host name will make your agent provide the right identity to github when you log in via ssh in that sandbox.
也使用名称github.com
:
Also using the name github.com
:
如果您还想使用主机名github.com
,即直接克隆git@github.com/...
,则必须担心ssh代理显示ssh密钥的顺序.使用ssh-add
添加身份的顺序似乎无关紧要.就我而言,.ssh/id_rsa
是我的代理提供的默认密钥,但并非总是如此.
If you also want to use the host name github.com
, i.e., cloning git@github.com/...
directly, then you have to worry about the order in which your ssh agent will present the ssh keys. The order in which you add your identities using ssh-add
does not seem to matter. In my case .ssh/id_rsa
was the default key my agent provided, but that's not always the case.
如果您使用的是gnome-keyring
,则在此问题中使用单独文件夹的建议似乎有所帮助:
If you're using gnome-keyring
, the suggestion to use a separate folder in this question seems to help: https://unix.stackexchange.com/questions/429730/how-to-change-the-order-of-keys-in-gnome-keyring
但是,使用ssh代理的Cygwin/OpenSSH安装,我无法将.ssh/id_rsa_2
设置为默认值.
However, using my Cygwin/OpenSSH installation of the ssh agent, I could not make .ssh/id_rsa_2
the default.
因此,在我的情况下,我必须在.ssh/config
中使用一个命名条目才能将.ssh/id_rsa_2
与GitHub一起使用,但是当我想要主键.ssh/id_rsa
时,可以使用名称github.com
.
So in my case, I have to use a named entry in .ssh/config
to use .ssh/id_rsa_2
with GitHub, but I can use the name github.com
when I want .ssh/id_rsa
, my primary key.
这篇关于当我尝试使用ssh推送时,Git尝试使用全局用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!