推送到.git但拒绝错误的用户,终于解决了 [英] Push to .git but denied to wrong user , finally solved

查看:352
本文介绍了推送到.git但拒绝错误的用户,终于解决了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常搜索.但是执行"git push -u origin master"后仍然报告错误. 最后,我成功执行了'git push -u origin master',但是我真的不知道错误在哪里.请耐心阅读以下内容.

I googling a lot. but it still report an error after execute 'git push -u origin master'. Finally I execute 'git push -u origin master' success, but I really don't know where the error is. Please read on patiently the content as follow.

1,我有一个git帐户"HelenZeng",现在我有另一个帐户"Snowbabe"

1, I've had a git account 'HelenZeng', now I have another account 'Snowbabe'

2,我生成了两个ssh密钥,一个是id_rsa_helen,另一个是id_rsa_snow

2, I generate two ssh key, one is id_rsa_helen, another is id_rsa_snow

3,我触摸〜/.ssh/config

3, I touch ~/.ssh/config

#github.snowbabe
  Host github.com-Snowbabe
          HostName github.com
          User Snowbabe
          IdentityFile ~/.ssh/id_rsa_snow

  #github.helenzeng
  Host github.com-HelenZeng
          HostName github.com
          User HelenZeng
          IdentityFile ~/.ssh/id_rsa_helen

4,老实说,我尝试过谷歌搜索,我真的不知道为什么要使用ssh-add

4, I have tried googling, to be honest, I really don't know why use ssh-add

$ ssh-add ~/.ssh/id_rsa_helen
$ ssh-add ~/.ssh/id_rsa_snow
$ ssh-add -l
  4096 SHA256:Ky5cNi8JKMUuguV0Snt4epmwFn2MgAk19nzF3MJujWU 
  /Users/'username'/.ssh/id_rsa_helen (RSA)
  4096 SHA256:hMfQyemWSz+f+AHrmsbZdhrDb11U+wA4xXeG3X1u28E 
  /Users/'username'/.ssh/id_rsa_snow (RSA)

5,我创建了一个React应用项目,我想将其推送到gitHub仓库(到git帐户"Snowbabe").

5, I created a react app project, and I want to push it to gitHub repository( to git account 'Snowbabe').

$ git init
$ git remote add origin git@github.com:Snowbabe/React-study.git

6,这是我的.git/config

6, Here is my .git/config

[remote "origin"]
       url = git@github.com:Snowbabe/React-study.git
       fetch = +refs/heads/*:refs/remotes/origin/*

正确的网址

7,最后一步

$ git push -u origin master
  ERROR: Permission to Snowbabe/React-study.git denied to HelenZeng.
  fatal: Could not read from remote repository.

  Please make sure you have the correct access rights

尝试解决:

$ ssh-add -l
 4096 SHA256:Ky5cNi8JKMUuguV0Snt4epmwFn2MgAk19nzF3MJujWU 
 /Users/'username'/.ssh/id_rsa_helen (RSA)
 4096 SHA256:hMfQyemWSz+f+AHrmsbZdhrDb11U+wA4xXeG3X1u28E 
 /Users/'username'/.ssh/id_rsa_snow (RSA)

/** delete all **/
$ ssh-add -D
 All identities removed.

/** just ssh-add id_rsa_snow **/
$ ssh-add ~/.ssh/id_rsa_snow
$ ssh-add -l
 4096 SHA256:hMfQyemWSz+f+AHrmsbZdhrDb11U+wA4xXeG3X1u28E 
 /Users/yaxian/.ssh/id_rsa_snow (RSA)

现在,执行'git push -u origin master'

Now, execute 'git push -u origin master'

$ git push -u origin master
Counting objects: 18, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (18/18), done.
Writing objects: 100% (18/18), 165.52 KiB | 0 bytes/s, done.
Total 18 (delta 0), reused 0 (delta 0)
To github.com:Snowbabe/React-study.git
 * [new branch]      master -> master

成功.

推荐答案

我想您想知道ssh-add的作用,以及为什么第一种使用ssh-add的方法失败而第二种方法成功了.我正在考虑这个假设.

I think you want to know what ssh-add does, and why the first way to use ssh-add failed while the second way succeeded. I'm answering with that assumption in mind.

ssh-add为何起作用

Why ssh-add works as it does

ssh-add用于在ssh代理中加载特定身份.您有几个可用的身份,并且您的ssh代理会以我不完全了解的顺序将它们提供给您尝试连接到的任何服务器.

ssh-add is used to load specific identities in your ssh agent. You have several identities available to you, and your ssh agent will offer them to any server you try to connect to, in an order I don't fully understand.

在典型情况下,给定的服务器将仅接受您的身份之一.例如,假设我添加了id_rsa_server1连接到host1,并添加了id_rsa_server2连接到host2.当我连接到host1时,ssh向服务器提供id_rsa_server1并成功连接.当我连接到host2时,ssh首先提供id_rsa_server1,但失败了,然后尝试下一个is_rsa_server2,现在又成功了.

Under typical circumstances, a given server would only accept one of your identities. For example, let's say I added id_rsa_server1 to connect to host1, and id_rsa_server2 to connect to host2. When I connect to host1, ssh offers id_rsa_server1 to the server and connects successfully. When I connect to host2, ssh first offers id_rsa_server1, which fails, then tries the next one, is_rsa_server2, which now succeeds.

当您加载两个身份时,您的问题是github.com既知道海伦又知道雪.您想用snow登录,因为那是拥有该特定存储库权限的帐户,但是ssh层会通过第一个接受的密钥helen成功运行,该密钥没有必需的权限.

Your problem when you loaded the two identities is that github.com knows both helen and snow. You want to log in with snow, since that's the account that has permissions on that particular repo, but the ssh layer succeeds with the first accepted key, helen, which doesn't have the required permissions.

因此,当您卸载身份并仅加载正确的身份时,事情就起作用了,因为这一次您以大雪的身份登录github.com并具有执行推送所需的权限.

So when you unload your identities and load only the right one, things work because this times you log in to github.com as snow, with the required permissions to do the push.

我希望这有助于弄清正在发生的事情.

I hope this helps clarify what is going on.

使用.ssh/config

Alternative and recommended solution with .ssh/config

因为您要使用该名称连接到github.com,所以我认为您的.ssh/config没有任何作用.仅当您将github.com-Snowbabe作为服务器名称连接时,它才会起作用.

Because you're connecting to github.com by that name, I think your .ssh/config does not do anything. It would only do something if you connected to github.com-Snowbabe as the server name.

这应该有效:

.ssh/config和User = git:

Host github.com-Snowbabe
   HostName github.com
   User git
   IdentityFile ~/.ssh/id_rsa_snow

将您的Git存储库设置为使用该远程名称:

set your Git repo to use this remote name:

git remote set-url origin github.com-Snowbabe/React-study.git

使用这些设置,您的ssh代理将在使用该主机名时使用正确的身份文件:在.ssh/config中定义的别名指定用户名(在github.com上始终为git)和身份(Snow的RSA)钥匙).您仍然需要像以前一样ssh-add id_rsa_snow,但是此配置会覆盖通过ssh连接时首先提供的身份.

With these settings, your ssh agent will use the right identity file when using that host name: the alias defined in .ssh/config specifies the user name (must always be git on github.com) and identity (Snow's RSA key). You still need to ssh-add id_rsa_snow as before, but this configuration overrides what identity is offered first upon connecting via ssh.

在回答

I've now tested this procedure well, in the context of answering a similar question, and corrected some mistakes above in the process.

这篇关于推送到.git但拒绝错误的用户,终于解决了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆