导航

Windows配置多个git用户

Posted on 2017-11-28 10:09  水木山川  阅读(3770)  评论(0编辑  收藏  举报

Window配置多个Git账户,SSH连接GitHub、GitLab

最新版本GIt配置对应多个Git仓库(不需要添加多个用户名和邮箱):

  在本地git上添加一个用户名和邮箱,生成一对公钥和私钥,把公钥加入到各个配置SSH key里面。

1.检查本机是否有ssh key设置,切换到.ssh目录
$ cd ~/.sshcd .ssh

2.配置git用户名和邮箱,配置多个用户时添加 --add 参数
$ git config --global --add user.name "username"
$ git config --global --add user.email "email"

3.查看用户名和邮箱
$ git config --list

4.生成github.com,gitlab.com对应的私钥公钥.
$ ssh-keygen -t rsa -C "test@qq.com"
注:生成id_rsa私钥公钥时需要命不同文件名,密码可设可不设。

5.添加ssh key到对应的GitHub,GitLab上面
登录GitHub系统;点击右上角账号头像的“▼”→Settings→SSH kyes→Add SSH key。
复制id_rsa.pub的公钥内容到Key填写框内。

$ssh -T git@github.com  测试公钥配置是否成功

6.在.ssh下面建立配置文件,文件名为config的文件且不需要添加文件后缀。
config文件配置内容:

# 配置github.com
Host https://github.com/ 
HostName https://github.com/
IdentityFile C:/Users/zengsm/.ssh/id_rsa
PreferredAuthentications publickey
User username1

# 配置git.gitlab.com
Host http://gitlab.zsm.com/
HostName http://gitlab.zsm.com/
IdentityFile C:/Users/zengsm/.ssh/id_rsa_gitlab
PreferredAuthentications publickey
User username2

  HostName           真实的域名地址

  IdentityFile           id_rsa的地址
  PreferredAuthentications    配置登录时用什么权限认证--可设为publickey,password publickey,keyboard-interactive等
  User                配置使用用户名

# 可以通过 ssh-add -l 来确私钥列表
$ ssh-add -l
# 可以通过 ssh-add -D 来清空私钥列表
$ ssh-add -D

7.克隆代码:
从刚才配置的github上克隆项目:  git clone git@配置的别名:用户名/项目名.git
(例:git clone git@github.com:username/projectname.git)

备注:
作者:Shengming Zeng
博客:http://www.cnblogs.com/zengming/

本文是原创,欢迎大家转载;但转载时必须注明文章来源,且在文章开头明显处给明链接。
<欢迎有不同想法或见解的同学一起探讨,共同进步>