git多账户使用

由于公司和个人分别有git账号,而git客户端在默认情况下是只能使用一个账号,如果需要使用多个账号,需要额外的设置。

 

1. 不使用全局设置,使用如下清除全局设置

  git config --global --unset user.name "your-name"

  git config --global --unset user.email "your-email"

 

2. 如果之前是使用ssh进行连接获取的,还需要将之前默认生成的秘钥删除

  rm ~/.ssh/id_rsa.pub

  rm ~/.ssh/id_rsa

 

3. 切换到对应项目的路径下,然后设置名称和邮箱

  git config user.name "your-name"

  git config user.email "your-email"

 

4. 生成ssh秘钥及其存放文件

  ssh-keygen -t rsa -C "your-email" -f " the generated file name"

5. 让git识别新生成的秘钥

  ssh-add "the generated file name"

注意:

  在第5步时可能会报Could not open a connection to your authentication agent.

  出现这个错误,那么在执行第5步前分系统先执行如下

    linux:

      ssh-agent bash

    windows:

      先在任务管理器下查看是否有ssh-agent进程存在,有则杀掉,再执行下面的语句

      eval $(ssh-agent -s).

6. ssh配置

  在~/.ssh路径下查看是否有config文件,若无,创建一个,然后配置config文件内容

  #第一个github项目账号

  Host first_project

    HostName IPADDRESS OR DOMAIN    eg.github.com

    User first_user

    IdentityFile "the generated file name1"

  #第二个git项目账号

  Host second_project

    HostName IPADDRESS OR DOMAIN

    User second_user

    IdentityFile "the generated file name2"

以上步骤配置好后多个git账号就能使用了

从first项目拉取文件

  git pull first master

posted on 2016-05-13 20:19  学习与成神之路  阅读(350)  评论(0编辑  收藏  举报

导航