Git多用户管理
在Git使用过程中,会遇到多用户权限验证问题,此处做一个简单介绍;
1、生成公钥
2、github ssh绑定(或gitee)
3、git ~/.ssh/ 目录下config文件配置
4、关联项目并配置本地仓库用户
1、Git公钥生成示例,分别生成github和gitee的公钥
#github公钥
$ ssh-****** -t rsa -C “[email protected]” -f ~/.ssh/id_github_rsa , "id_github_rsa"为公钥的名称
#gitee公钥
$ ssh-****** -t rsa -C “[email protected]” -f ~/.ssh/id_gitee_rsa , "id_gitee_rsa "为公钥的名称
2、github ssh**管理
#进入本地**目录
$ cd ~/.ssh/
#打开刚刚生成的**
$ cat id_github_rsa.pub
将上图红框的**拷贝到GitHub的绑定输入框,点击添加
3、配置本地git config文件
#打开.ssh/ 目录
$ cd ~/.ssh/
#查看是否有config文件,没有则
创建 $ touch config
#编辑config配置文件
$ vim config
#输入一下内容
#github --- github
Host gitee.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_github_rsa
User github
#gitee --- gitee
Host gitee.com
HostName gitee.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_gitee_rsa
User gitee
#config配置文件(注:无后缀名),配置规范如下(可配置多个git账号):
#Host host(Host简称,使用命令ssh host可连接远程服务器,如:ssh github)
#User/Email 登录用户名(如:zlzsam/[email protected])
#HostName 主机名用ip或域名,建议使用域名(如:github.com)
#Port 服务器open-ssh端口(默认:22,默认时一般不写此行
#IdentityFile 证书文件路径(如~/.ssh/id_rsa_*)
#如Host 写成 Host luofeng_github,那么在关联项目的时候需将前缀修改:
[email protected]:luowuji/SpringBootPractice.git
#remote的时候修改成 github.com —> luofeng_github
[email protected]_github:luowuji/SpringBootPractice.git
#测试是否可以正常访问到github
$ ssh -T [email protected]
#显示如下,表示验证通过
Hi luowuji! You've successfully authenticated, but GitHub does not provide shell access.
4、关联项目并配置本地仓库用户
#关联项目
$ git remote origin master [email protected]:luowuji/SpringBootPractice.git
#设置用户,不同的仓库需要设置不同的用户名和邮箱,如果之前有设置需要unset
$ git config --global --unset user.name
$ git config --global --unset user.email
#然后在不同的仓库下设置
$ git config user.name "yourname"
$ git config user.email "youremail"
git命令 查看gitconfig信息
$ git config --list #查看全局git配置
$ git config --local --list #查看单仓git配置