Problem
工作的代码都放到公司的GitLab中,个人平时也会是会GitHub,如果按照默认的配置方法,两者会冲突。
Solution
- 秘钥文件区分存放
- GitLab按照默认方式存放
ssh-keygen -t rsa -C "changyufei@che001.com"
,一路next - GitHub自定义存放,文件名为id_rsa_github
ssh-keygen -t rsa -C "yufei.chang@outlook.com"
,Enter file in which to save the key (/c/Users/bili/.ssh/id_rsa): /c/Users/bili/.ssh/id_rsa_github - 此时,
~/.ssh
目录下应该有4个文件
bili@c190198 MINGW64 ~/.ssh
$ ls
id_rsa id_rsa.pub id_rsa_github id_rsa_github.pub
- 配置ssh key,将pub文件中的内容放到GitLab和GitHub中
- 在
~/.ssh
下添加config配置文件
Host gitlab
HostName [IP or host]
IdentityFile ~/.ssh/id_rsa
Host github
HostName github.com
IdentityFile ~/.ssh/id_rsa_github
- 设置user.name和user.email
- 工作中GitLab使用频繁,将GitLab设置为global,在任意目录下
$ git config --global user.name "changyufei"
$ git config --global user.email "changyufei@che001.com"
- GitHub设为local,在GitHub的工作空间下
$ git config --local user.name "yufei.chang"
$ git config --local user.email "yufei.chang@outlook.com"
报错
$ git config --local user.name "yufei.chang"
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
文件后,再执行即可