美文网首页TECH_GIT
如何多账号连接到github,本机连接不再输入密码

如何多账号连接到github,本机连接不再输入密码

作者: 爱读书的阿啦嘞 | 来源:发表于2016-04-06 18:14 被阅读1934次

首先你要明白github连接过程是怎样的。


私有密钥和公有密钥是成对的两个文件,私有文件保存在自己的本机,公有密钥保存到另一端的服务器,网站等。
github就是一种网站。只有保存了私有密钥的机器才能访问远程的服务器等。使用该键的好处是不用使用密码,而是以密钥的方式验证用户。


https 和 SSH 的区别:

  • 前者可以随意克隆github上的项目,而不管是谁的;而后者则是你必须是你要克隆的项目的拥有者或管理员,且需要先添加 SSH key ,否则无法克隆。

  • https url 在push的时候是需要验证用户名和密码的;而 SSH 在push的时候,是不需要输入用户名的,如果配置SSH key的时候设置了密码,则需要输入密码的,否则直接是不需要输入密码的。


要想使本机能访问github。有四个步骤:

  1. 创建私有密钥和公有密钥
  2. 将公有密钥放到github里。
  3. 测试是否设置成功。
  4. 修改本地git配置文件,发布。

1. 创建私有密钥和公有密钥

1.1 首先判断本机是否创建了公有密钥:

$ ls ~/.ssh

这个命令用于检查是否已经存在 id_rsa.pub 或 id_dsa.pub 文件,如果文件已经存在,下面步骤可省略,直接进入步骤2。

如果没有类似 id_rsa和id_rsa.pub这样的文件,则表明没有创建。生成的办法:

//配置git用户名和邮箱:
$ git config user.name "用户名"
$ git config user.email "邮箱" 
$ ssh-keygen -t rsa -C "邮箱" 
//多个密钥的情况下,可生成ssh key同时指定保存的文件名
$ ssh-keygen -t rsa -f ~/.ssh/ellacf -C "邮箱" 

代码参数含义:

  • -t 指定密钥类型,默认是 rsa ,可以省略。
  • -C 设置注释文字,比如邮箱。
  • -f 指定密钥文件存储文件名。

执行后,会填写保存两种密钥的文件夹,和passphrase,全部可以按enter。然后执行ls来查看生成后的文件。

  • id_rsa和id_rsa.pub分别是私有密钥和公有密钥。
  • 我们指定的文件名就是id_rsa.github,这时~/.ssh目录下会多出id_rsa.github和id_rsa.github.pub两个文件,id_rsa.github里保存的就是我们要使用的key。

1.2 多个密钥情况下,可以:

  • 新增并配置config文件。如果config文件不存在,先添加;存在则直接修改
$ touch ~/.ssh/config
  • 在config文件里添加如下内容(User表示你的用户名)
Host *.github.com
    IdentityFile ~/.ssh/id_rsa.github
    User 用户名

2. 将公钥添加到github上

2.1 首先你需要拷贝 id_rsa.pub 文件的内容,你可以用编辑器打开文件复制,也可以用git命令复制该文件的内容,如:

$ pbcopy < ~/.ssh/ellacf.pub

添加到Github的ssh kesy设定里。
1. 复制key到剪贴板
2. 登录github
3. 点击右上方的Accounting settings图标
4. 选择 SSH key
5. 点击 Add SSH key


3. 测试github是否连接成功

3.1 接下来进行测试:

$ssh -T git@github.com

如果显示:

Are you sure you want to continue connecting (yes/no)?
输入yes。

然后就可以看到

Hi yourusername! You've successfully authenticated, but GitHub does not
provide shell access.

这样就可以通过ssh方式clone Github上的工程并且进行pull和push了。

3.2 总结:

如果在步骤3的ssh命令后或者输入yes后出现github Permission denied错误。执行以下命令:

//start the ssh-agent in the background
$eval "$(ssh-agent -s)"
$ssh-add ~/.ssh/id_rsa

再执行

$ssh -T git@github.com

4. 修改本地git配置文件,发布

4.1 修改本地配置文件,.git文件夹下的config文件

修改前

[remote "origin"]
    url = https://github.com/Ellacf/helloworld
    fetch = +refs/heads/*:refs/remotes/origin/*

修改后

[remote "origin"]
    url = git@github.com:Ellacf/helloworld.git
    fetch = +refs/heads/*:refs/remotes/origin/*

发布

修改README.MD
$ git add .   //更新README文件
$ git commit -m 'first commit'//提交更新,并注释信息“first commit” 
$ git remote add origin git@github.com:Ellacf/helloworld.git   //连接远程github项目  
$ git push -u origin master   //将本地项目更新到github项目上去

5. 关于可能出现的错误

5.1 在执行 $ git remote add origin git@github.com:Ellacf/helloworld.git

错误提示:fatal: remote origin already exists.

解决办法:
$ git remote rm origin   //删除远程路径

然后在执行:
$ git remote add origin git@github.com:Ellacf/helloworld.git  //添加正确路径
就不会报错误了

5.2 在执行 $ git push origin master


错误提示:error:failed to push som refs to.......

解决办法:

$ git pull origin master // 先把远程服务器github上面的文件拉下来,再push 上去。

5.3 执行 git remote -v,即可看到远处资源库路径,如下所示:

origin  git@github.com:Ellacf/helloworld.git (fetch)
origin  git@github.com:Ellacf/helloworld.git (push)

5.4 隐私设置

当公司全局设置git时,发布后会显示公司配置的相应用户名和邮箱。此时需要针对每个项目,单独设置用户名和邮箱,设置方法如下:

git clone https://github.com/Ellacf/helloworld.git // git检出目录  
cd ~/helloworld  
git init  
git config user.name "用户名"  
git config user.email "邮箱"
git remote rm origin
git remote add origin git@github.com:Ellacf/helloworld.git

6.修改配置文件~/.ssh/config文件

6.1 在命令行输入如下:

touch ~/.ssh/config
vim ~/.ssh/config

6.2 按住键盘i,进入文件编辑模式,写入如下:

Host github.com
       Hostname github.com
       User Ellacf
       Identityfile ~/.ssh/ellacf

6.3 输入:wq,保存文件并退出即可。


参考我的配置在第一个留言。
以上更新于:2017-07-30

相关文章

网友评论

  • 爱读书的阿啦嘞:我的配置信息如下:
    1.~/.ssh/config文件(用于ssh -T git@github.com时验证)

    ```
    Host github.com
    Hostname github.com
    User Ellacf
    Identityfile ~/.ssh/ellacf
    # helloworld
    Host helloworld.github.com
    HostName github.com
    User Ellacf
    Identityfile ~/.ssh/ellacf
    # react-native
    Host reactnative.github.com
    HostName github.com
    User Ellacf
    Identityfile ~/.ssh/ellacf
    ```

    2.git remote -v,查看远程ssh配置
    ```
    分别在helloworld和react-native-basic目录下执行
    git remote add origin git@github.com:Ellacf/helloworld.git
    git remote add origin git@github.com:Ellacf/react-native-basic.git
    查看git remote -v后的结果,分别如下:
    //helloworld
    origin git@github.com:Ellacf/helloworld.git (fetch)
    origin git@github.com:Ellacf/helloworld.git (push)
    //react-native-basic
    origin git@github.com:Ellacf/react-native-basic.git (fetch)
    origin git@github.com:Ellacf/react-native-basic.git (push)
    ```

    3.修改项目中任意文件,进行发布时,执行
    git push -u origin master

本文标题:如何多账号连接到github,本机连接不再输入密码

本文链接:https://www.haomeiwen.com/subject/brdmlttx.html