Git多帐号问题

git多个帐号的问题

由于我平时使用的github和公司的gitlab所用的用户名以及邮箱不同。所以要设置git多帐号登录。

取消git全局设置

网上的教程中大都是对git进行全局设置,如下所示:

git config --global user.name "your_name"
git config --global user.email "your_email"

如果参与的项目都允许你用同一个用户名和邮箱,这样设置没问题,但是在公司里,大都会配置相应的域帐号和公司邮箱,因此我们首先需要取消git的全局设置:

git config --global --unset user.name
git config --global --unset user.email
SSH配置

假设 有两个git项目,使用用户名分别为A/B,用的邮箱分别为C/D。则分别进行如下:

  • 在~/.ssh目录下,使用ssh-keygen -C "your_email" -t rsa生成公钥和私钥,命名分别为id_rsa_first,id_rsa_second (注意不要按照默认配置命名),并需要将公钥的内容分别上传到对应git项目的服务器上。

  • 在~/.ssh目录下创建或修改config文件,并进行相应配置。

我的config文件配置代码如下:

#该文件用于配置私钥对应的服务器

#github.com上的帐号
#建立github.com的简称,使用这个别名做克隆和更新
Host    Github
# 主机名可用ip也可以用域名
HostName    github.com
User    kungeplay
IdentityFile    ~/.ssh/id_rsa_github

#gitlab上的帐号
Host    Gitlab
HostName    gitlab.corp.XXX.com
User    jiakun.liu
IdentityFile    ~/.ssh/id_rsa_gitlab

#oschina上的帐号
Host    Oschina
HostName    git.oschina.net
User    kungeit
IdentityFile    ~/.ssh/id_rsa_oschina
配置Git项目仓库

针对每个项目单独设置用户名和邮箱:进入到各自git项目repository的相对根目录下,通过git config配置用户名和密码。

比如我用git init在工作目录中初始化新仓库时:

mkdir Git_Hub/Shell_Practice && cd Git_Hub/Shell_Practice
git init
git config user.name "your_name"
git config user.email "your_email"
git remote add origin git@Github:/kungeplay/Shell_Practice.git
git pull origin master

再比如我用git clone从现有仓库中克隆一个新仓库时:

cd ~/Git/Git_Lab/
git clone git@Gitlab:jiakun.liu/second_gitlab.git
cd second_gitlab/
git config user.name "your_name"
git config user.email "your_email"
vim NewFile
git  status
git add NewFile 
git commit -m "这是一个新文件"
git push  origin master

上述的git config命令没有加”–global”参数,因而是针对具体本地git项目repository目录的。这些配置的优先级高于全局配置。

注意在clone或者add remote的时候,需要使用.ssh目录中的config文件中的Host代替真实的git@remoteAddress中的remoteAddress,这样才能让git识别出来。比如config文件中设置的关于github上的Host为Github,则:

git clone git@Github:kungeplay/Shell_Practice.git

其他一些不涉及具体仓库的可以全局配置。比如

git config --global color.ui true   # 彩色的git输出
git config --global push.default simple # 执行git push没有指定分支时,只有当前分支会被push到你使用 git pull获取的代码
git config --global core.autocrlf false # 让Git不要管Windows/Unix换行符转换的事
git config --global gui.encoding utf-8  # 避免git gui中的中文乱码
git config --global core.quotepath off  # 避免git status显示的中文文件名乱码
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值