Git(二) getlab账号和github账号同时使用

Git(二) getlab账号和github账号同时使用在一台电脑

上篇文章主要说到Git的Git介绍和安装;本篇文章接着上篇内容继续为大家介绍 Git的使用:getlab账号和github账号同时使用。

前言

一、windows电脑,没有 .ssh 文件怎么办?

二、不同密钥文件生成

三、配置密钥文件

范围配置


一,windows电脑,没有 .ssh 文件怎么办?

我们在工作中用到的Git 有两种服务器的账号:

1. 免费的 Github(个人到官网注册账号)

2. 自己搭建的Gitlab(一般企业专门搭建)

一台电脑如果要同时使用github账号和gitlab账号更新代码,必须需要通过密钥区分和设置,在我们配置和生成密钥的时候,windows需要有.ssh的支持,比如:windows电脑连接GitHub,需要设置密钥,电脑没有.ssh文件夹,这样就需要我们生成该文件夹。

生成方法如下:
如果是第一次使用github,先去官网注册,注册后有用户名,邮箱和密码。

找到安装git目录,打开GIT bash


写上命令:
1.git config --global user.name “XXX”
xxx代表 用户名

2.git config --global user.email "[email protected]"
输入邮箱

3.生成一个新的SSH密钥
打开 Git Bash,输入如下命令,
ssh-keygen -t rsa -C "[email protected]"   ,其中 [email protected] 为你注册github账号的邮箱

然后连续按三个回车,会有提示Enter file in which to save the key 后面是路径,你就会找到你想要的.ssh文件夹了,去电脑里找吧。

一般目录如下 : C:\Users\Administrator\.ssh 

当然还有一些电脑目录是这样的,如下图:

2,不同密钥文件生成

  • GitLab按照默认方式存放ssh-keygen -t rsa -C "[email protected]",一路next
  • GitHub自定义存放,文件名为id_rsa_github ,ssh-keygen -t rsa -C "[email protected]",Enter file in which to save the key (/c/Users/lenovo/.ssh/id_rsa): /c/Users/lenovo/.ssh/id_rsa_github
  • 此时,~/.ssh目录下应该有4个文件

操作步骤如下图:

生成文件如下;名字可以自己定义。

三、配置密钥文件

  1. 配置ssh key,将pub文件中的内容放到GitLab和GitHub中(需要到GitLab和GitHub服务器就行配置,具体配置百度一下,资料很多操作简单)
  2. ~/.ssh下添加config配置文件
Host gitlab
    HostName [IP or host]        
    IdentityFile ~/.ssh/id_rsa
Host github
    HostName github.com
    IdentityFile ~/.ssh/id_rsa_github

四,范围配置

  1. 设置user.name和user.email
  • 工作中GitLab使用频繁,将GitLab设置为global,在任意目录下
$ git config --global user.name "chen"
$ git config --global user.email "[email protected]"
  • GitHub设为local,在GitHub的工作空间下
$ git config --local user.name "chendz"
$ git config --local user.email "[email protected]"

操作如下图:

如果发生如下报错

$ git config --local user.name "chen"
error: could not lock config file .git/config: No such file or directory
error: could not lock config file .git/config: No such file or directory

解决办法:

在该目录下创建.git/config文件后,再执行即可

OK,本文我们就先说到这里,有问题欢迎留言讨论。,下一章:git工具的使用Sourcetrue。

                                                                                                     -END-

                                                                                                   程序职场

                                                                                           一个执着的职场程序员

发布了55 篇原创文章 · 获赞 101 · 访问量 34万+

猜你喜欢

转载自blog.csdn.net/jianpengxuexikaifa/article/details/101435077