Git 生成 && 配置SSH key,配置多個SSH key

一、Git 生成 && 配置SSH key

步驟:

1.配置姓名和郵箱

git config --global user.name "xb12369"
git config --global user.email "[email protected]"

2.生成密鑰,一路回車,

ssh-keygen -t rsa -C "[email protected]"

生成密鑰成功,如下圖
在這裏插入圖片描述
3.添加SSH key到GitHub上

打開id_rsa.pub,把id_rsa.pub文件的內容即密鑰放到github上。
GitHub -> Settings -> SSH and GPG keys,點擊 New SHH key,把id_rsa.pub文件的內拷貝到內容區,爲添加的SSH key 添加一個名稱,點擊Add SSH kye。添加成功

  1. 測試是否連接上GitHub
ssh -T [email protected]
//如果是gitlab ,就使用下面的語句
ssh -T [email protected]

二、生成多個ssh key,配置Gitlab 和 Github

配置多個git賬號:如gitlab和github,那個就配置兩個賬號:一個global全局的賬號,和配置一個local 當前項目範圍的賬號

步驟
  1. 配置姓名和郵箱
git config --local user.name "xb12369"
git config --local user.email "[email protected]"
  1. 生成密鑰
ssh-keygen -t rsa -C "[email protected]"

在這一步,關鍵:如果要配置多個SSH key, 會生成多個id_rsa文件,但是不能把之前的覆蓋了,於是要重新輸入文件名

$ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
//由於之前生成全局的賬號時已經生成了/id_rsa文件,於是要重新命名一個文件,此處輸入新的文件路徑和文件名
Enter file in which to save the key (/d/MyConfiguration/TCLDUSER/.ssh/id_rsa): /d/MyConfiguration/TCLDUSER/.ssh/id_rsa_github
//密碼回車不要輸入
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /d/MyConfiguration/TCLDUSER/.ssh/id_rsa_github.
Your public key has been saved in /d/MyConfiguration/TCLDUSER/.ssh/id_rsa_github.pub.
  1. 添加SSH key到GitHub上,同上面的操作

  2. 測試是否連接上GitHub


三、Git 連接遠程庫失敗:倉庫不被授權的問題

重點:在git bash 命令行在添加私鑰。解決報錯的步驟主要是這一步,應該添加私鑰

  1. 查看私鑰列表的命令爲:
ssh-add -l
  1. 回車後,如果報錯,輸出以下命令出現
Could not open a connection to your authentication agent.
  1. 則再輸入下面命令,即可進入SSH bash
ssh-agent bash
  1. 然後添加私鑰,命令如下:
ssh-add ~/.ssh/id_rsa
  1. 此時再查看私鑰列表:ssh-add -l,若已經成功添加,則可以正常地使用 git push 進行對遠程倉庫的更新了。

  2. 測試是否連接上GitHub

ssh -T [email protected]
//如果是gitlab ,就使用下面的語句
ssh -T [email protected]

此時應該是成功連接,會提示下面的內容

You've successfully authenticated
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章