---
title: "git同时配置gitlab和github"
author: "cjh326419411"
date: "2016-06-16T16:27:05Z"
tags: []
categories: ["开源软件"]
slug: "git-gitlab-github"
draft: false
lastmod: "2017-04-21T18:57:05Z"
id: 2305534
url: "https://cjhz.iteye.com/blog/2305534"
---

**本人一字一句写的，转载备注来源，谢谢**

 

**一、设置gitlab秘钥：**

1、$ssh-keygen -t rsa -C "cjh@xxx.com"

2、回车使用默认 id\_rsa取名，最后会生成id\_rsa 与id\_rsa.pub两个文件

3、输入密码

4、cd ~/.ssh

5、vim id\_rsa.pub 拷贝到gitlab设置ssh keys

 

**二、设置github秘钥：**

1、$ssh-keygen -t rsa -C "cjhbest999@163.com"

2、输入id\_rsa\_github，如此会生成id\_rsa\_github 与id\_rsa\_github.pub两个文件

3、输入密码

4、cd ~/.ssh

5、vim id\_rsa\_github.pub 拷贝到github设置ssh keys

 

 如果出现 could not open a connection to your authentication agent的错误，执行如下：

    ssh-agent bash

    ssh-add id\_rsa\_xxx  (xxx替换具体的名字)

 

**三、配置config：**

1、cd ~/.ssh

2、touch config （新建config文件，没有后缀）

3、设置config内容：

* * *

#gitlab\_company

Host git.xxx.com

    HostName git.xxx.com

    IdentityFile ~/.ssh/id\_rsa

 

 

#github\_cjhz

Host github.com

    HostName github.com

    IdentityFile ~/.ssh/id\_rsa\_github

* * *

Host: 别名

HostName: 托管仓库的域名或者ip

IdendityFile: 秘钥的路径

 

**四、测试配置是否成功：**

1、ssh -T [git@git.xxx.com](mailto:git@git.qccrnb.com)

2、ssh -T [git@github.com](mailto:git@github.com)

注：此处的[git.xxx.com](mailto:git@git.qccrnb.com) 、[github.com](mailto:git@github.com) 是config内容的Host别名，别名可以随意设置

 

**五、设置用户名和邮箱**

1、git config -l

     如果开始设置过如：git config --global user.name "xxx"

                                  git config --global user.email "xxx@yy.com"

     需要去掉

2、去掉全局设置：

     git config --global --unset user.name

     git config --global --unset user.email

3、设置用户和邮箱

      进入到项目的根目录：cd ~/workspace/workspace\_1/wms

      git config user.name "cjh"

      git config user.email "cjh@xxx.com"

   github项目同理