歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> 多個GitHub帳號的SSH key切換

多個GitHub帳號的SSH key切換

日期:2017/2/28 13:52:29   编辑:Linux教程

一台電腦上有一個ssh key,在github上提交代碼,由於其他原因,你可能會在一台電腦上提交到不同的github上,怎麼辦呢...

假設你電腦上一個ssh key都沒有,如果有默認的一個了,請直接生成第二個

一、生成並添加第一個ssh key

$ ssh-keygen -t rsa -C "[email protected]"在Git Bash中執行命令一路回車,會在~/.ssh/目錄下生成id_rsa和id_rsa.pub兩個文件

用文本編輯器打開id_rsa.pub裡的內容,在Github中添加SSH Keys

不明白的請參考GitHub創建SSH Keys

二、生成並添加第二個ssh key

$ ssh-keygen -t rsa -C "[email protected]"這次不要一路回車了,給這個文件起一個名字 不然默認的話就覆蓋了之前生成的第一個

假如起名叫my,目錄結構如下:

如果生成的第二個ssh key不在.ssh/下,可移動到此目錄

三、在.ssh/下創建config文件 內容如下:
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Host my.github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/my
Host名字隨意,接下來會用到。

四、測試配置是否正確

如果出現Hi xxx!You've successfully authenticated 就說明連接成功了

現在就以下種情況給出不同的做法:
1、本地已經創建或已經clone到本地:

如下兩種解決方法:

打開.git/config文件

#更改[remote "origin"]項中的url中的
#my.github.com 對應上面配置的host
[remote "origin"]
url = [email protected]:itmyline/blog.git或者在Git Bash中提交的時候修改remote

$ git remote rm origin
$ git remote add origin [email protected]:itmyline/blog.git2、clone倉庫時對應配置host對應的賬戶

#my.github.com對應一個賬號
git clone [email protected]:username/repo.git作者:itmyhome

GitHub 教程系列文章

通過GitHub創建個人技術博客圖文詳解 http://www.linuxidc.com/Linux/2015-02/114121.htm

GitHub 使用教程圖文詳解 http://www.linuxidc.com/Linux/2014-09/106230.htm

Git 標簽管理詳解 http://www.linuxidc.com/Linux/2014-09/106231.htm

Git 分支管理詳解 http://www.linuxidc.com/Linux/2014-09/106232.htm

Git 遠程倉庫詳解 http://www.linuxidc.com/Linux/2014-09/106233.htm

Git 本地倉庫(Repository)詳解 http://www.linuxidc.com/Linux/2014-09/106234.htm

Git 服務器搭建與客戶端安裝 http://www.linuxidc.com/Linux/2014-05/101830.htm

Git 概述 http://www.linuxidc.com/Linux/2014-05/101829.htm

分享實用的GitHub 使用教程 http://www.linuxidc.com/Linux/2014-04/100556.htm

GitHub 的詳細介紹:請點這裡
GitHub 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved