關於git項目中多ssh-key管理

前言

git項目進行pull,push等操做時,都須要本地ssh-key權限。但當多個倉庫或多個帳號進行開發時,則須要對多個key進行管理。 環境: windows10, Git Bash。git

生成ssh-key

一、生成一個新的自定義名稱(demo)的公鑰:github

$ ssh-keygen -t rsa -C "YOUR_EMAIL@YOUREMAIL.COM" -f ~/.ssh/demo
複製代碼

執行命令後,一路回車;
執行完成後,會在 ~/.ssh/目錄下生成一個 demo 和 demo.pub(主要內容在此文件中) 文件;
而後可一樣步驟再生成另外一個公鑰(anohterDemo)。shell

二、添加相應的 demo.pub,anotherDemo.pub 到你的git項目上。windows

公鑰添加代理

一、查看系統ssh-key代理,爲公鑰添加代理,執行以下命令bash

$ ssh-add -l
複製代碼

以上命令若輸出 "Could not open a connection to your authentication agent" 則尚未建立代理。能夠先執行如下命令建立代理服務器

$ eval `ssh-agent -s`
複製代碼

如輸出 "The agent has no identities" 則表示沒有代理。若是系統有代理,能夠執行下面的命令清除代理:ssh

$ ssh-add -D
複製代碼

二、而後依次將不一樣的ssh添加代理,執行命令以下:ide

$ ssh-add ~/.ssh/demo
 $ ssh-add ~/.ssh/anotherDemo
複製代碼

配置公鑰對應服務器

一、配置 ~/.ssh/config 文件 若是沒有就在~/.ssh目錄建立config文件,該文件用於配置對應的服務器。測試

#demo resrepositories 
Host github-demo
HostName github.com
User git
IdentityFile C:/Users/用戶名/.ssh/demo

#anotherDemo resrepositories 
Host github-anotherDemo
HostName github.com
User git
IdentityFile C:/Users/用戶名/.ssh/anotherDemo
複製代碼

配置完成後,在鏈接github倉庫時,遠程庫的地址要對應地作一些修改,好比如今添加賬號下的demo倉庫,則須要這樣添加:ui

git remote add test git@github-demo:username/demo.git
#並不是原來的git@github.com:username/demo.git
複製代碼

二、測試 ssh

$ ssh -T git@github.com
複製代碼

你會獲得以下提示,表示這個ssh公鑰已經得到了權限

Hi @username/demo! You've successfully authenticated, but GitHub does not provide shell access. 複製代碼

Ps: 同理可得多帳號操做方式

相關文章
相關標籤/搜索