git多用户配置
场景
在git的使用中,经常会出现这种情况:在公司有一个git账户,github上有个人账户,希望在同一台电脑针对不同的项目使用不同的账号。
此时就需要进行git多用户配置
-
创建用户
- 全局用户
git config --global user.name "globalUser" git config --global user.email "global@email.com" - 局部用户
cd 到项目所在目录,或在项目目录打开git bashgit config user.name "workUser" git config user.email "work@email.com"
- 全局用户
-
生成ssh-key
针对不同账户保存不同文件名ssh-keygen -t rsa -C "global@email.com" Enter file in whict to save the key (/c/Users/Administrator/.ssh/id_rsa): id_rsa ssh-add ~/.ssh/id_rsassh-keygen -t rsa -C "work@email.com" Enter file in whict to save the key (/c/Users/Administrator/.ssh/id_rsa): work_id_rsa ssh-add ~/.ssh/work_id_rsa -
将生成的ssh-key添加到github和公司git仓库
查看密钥,注意添加后缀为pub的keycat ~/.ssh/id_rsa.pub -
在
~/.ssh目录下添加config文件cd ~/.ssh vim config #github global@gmail.com host github.com #别名,随便定 后面配置地址有用 Hostname github.com #要连接的服务器 User global #用户名 IdentityFile ~/.ssh/id_rsa #密钥文件的地址,注意是私钥 #github work host work #别名,随便定 Hostname 公司git仓库地址 Port 公司git仓库端口 User work IdentityFile ~/.ssh/work_id_rsa -
验证是否成功
ssh -T git@github.com ssh -T git@公司git仓库地址
本文详细介绍了如何在一台电脑上为不同的项目配置多个Git账户,包括设置全局和局部用户、生成SSH-key及添加到GitHub和公司仓库的过程。
1万+

被折叠的 条评论
为什么被折叠?



