更好的管理你的git SSH-Key

作者: 禹子歆 | 来源:发表于2017-01-07 16:42 被阅读166次

日常工作中,我们经常碰到又想写公司的项目,又想自己维护自己的项目,公司的项目基本都在gitlab上,自己的项目在github上,所以我们需要配置不同的SSH-Key来对应不同的环境。

1. 设置一个默认的全局name和email

git config --global user.name "your name"
git config --global user.email "your email addr"

如果不设置这个,已经clone过的每个项目都需要去手动设置一下归属到哪个账户(当然,如果你是用那个clone下来的倒没事,像我是已经clone过了,不得不设置啊)

2. 生成SSH-Key

$ ssh-keygen -t rsa -C "your company email" -f ~/.ssh/id_rsa

一般都叫id_rsa,第一个先弄公司的好了,然后再对应的公司的账户下边加上id_rsa.pub里的内容
再生成github的SSH-Key:

$ ssh-keygen -t rsa -C "your github email" -f ~/.ssh/my_rsa

同上,把my_rsa.pub里的内容加到github服务器的配置中。

3. 添加私钥

$ ssh-add ~/.ssh/id_rsa 
$ ssh-add ~/.ssh/my_rsa

上面一步执行如果失败,并且提示的是"Could not open a connection to your authentication agent",执行:

$ ssh-agent bash

添加成功过后,可以执行下面这条查看结果

$ ssh-add -l

4. 修改host配置

打开~/.ssh里的config文件(如果没有就新建一个,没有后缀名)
加上以下内容:

# gitlab
Host gitlab.com
    HostName 公司gitlab地址
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
    User 你想要的名字
# github
Host github.com
    HostName github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/github_rsa
    User 你github要显示的名字

测试一下:

$ ssh -T git@github.com

也可以试试公司的网址,如果出现Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.就可以了。

5. 对应账号

如果已经设置了global的用户名和email,就得到不想用默认账户对应的项目文件夹下执行

$ git config user.name "your github name"
$ git config user.email "your github email"

这样就可以使用github的账号了。如果不想有默认的而且设置过,可以执行

$ git config --global --unset user.name
$ git config --global --unset user.email

6. 对于本博客的修改

在博客目录下边的.deploy_git下边里有config文件,这是hexo提交的时候对应的config文件,可以把.git下边的config里的user拷过来,顺便把这个url改成ssh的。

相关文章

  • 更好的管理你的git SSH-Key

    日常工作中,我们经常碰到又想写公司的项目,又想自己维护自己的项目,公司的项目基本都在gitlab上,自己的项目在g...

  • Github使用笔记

    一、配置Github环境 安装Git 配置ssh-key 检查ssh-key的设置 生成新ssh-key 添加ss...

  • Git配置多个SSH-KEY

    git可以配置多个ssh-key。 生成多个ssh-key命令 添加私钥

  • 指定ssh key文件访问git

    指定ssh-key文件访问git 本地ssh-key文件放置一个固定目录,/temp/ssh-key.pem 在~...

  • GIT的基本使用

    git生成SSH-KEY 生成的ssh-key可以复制到网站账户上面,这样就可以通过ssh的方式提交代码,也可以复...

  • Git使用相关

    git 配置多个SSH-Key生成并部署SSH key Git 忽略一些文件不加入版本控制: 在Git中如果想忽略...

  • git

    创建git账号开启远端账户 在自己的电脑当中生成ssh-key ssh-keygen 绑定到git账户 在gith...

  • 代码库迁移

    1、申请空的仓库 2、配置ssh-key 3、clone旧代码到本地 4、修改config git remote ...

  • git clone 指定 ssh-key 文件

    环境 & 软件 mac OS 问题 git clone 不是默认 ssh-key,无法克隆 解决方法 用ssh-a...

  • ssh-key git多账户配置

    在使用git的时候,一般我们使用的远程Git服务器是github,这时只需简单的生成ssh-key密钥对并将公钥添...

网友评论

    本文标题:更好的管理你的git SSH-Key

    本文链接:https://www.haomeiwen.com/subject/lnufbttx.html