Git中如何利用生成SSH個人公鑰訪問git倉庫方法(這里以coding平台為例):
1. 獲取 SSH 協議地址
在項目的代碼頁面點擊 SSH 切換到 SSH 協議, 獲得訪問地址, 請使用這個地址來訪問您的代碼倉庫,如下圖:
2. 生成公鑰
Mac/Linux 打開命令行終端, Windows 打開 Git Bash 。 輸入ssh-keygen -t rsa -C “username@example.com”,( 注冊的郵箱),接下來點擊enter鍵即可(也可以輸入密碼)。
$ssh-keygen -t rsa -b 4096 -C "your_email@example.com" // 按回車enter # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
// 推薦使用默認地址,如果使用非默認地址可能需要配置 .ssh/config ,在前面標下划的對應目錄下可以看到生成的公鑰文件
成功之后
Your identification has been saved in /Users/you/.ssh/id_rsa. # Your public key has been saved in /Users/you/.ssh/id_rsa.pub. # The key fingerprint is: # 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
3. 在 Coding.net 添加公鑰
本地打開 id_rsa.pub 文件(或執行 $cat id_rsa.pub ),復制其中全部內容,添加到賬戶“SSH 公鑰”頁面 中,公鑰名稱可以隨意起名字。
- 完成后點擊“添加”,然后輸入密碼或動態碼即可添加完成。
- 完成后在命令行測試,首次建立鏈接會要求信任主機。
$ ssh -T git@git.coding.net // 按回車enter
Enter passphrase for key ‘/c/Users/Yuankai/.ssh/id_rsa’: Coding.net Tips : [ Hello Kyle_lyk! You have connected to Coding.net by SSH successfully! ] `
注意: 同一個公鑰只能綁定一個賬戶,Coding 暫時不支持同一公鑰綁定多個賬戶。
如果需要使用多個賬戶建議生成多個公鑰,可以在.ssh/config文件中加上下邊一段
Host git.coding.net User xxxx@email.com PreferredAuthentications publickey IdentityFile ~/.ssh/coding_rsa // 生成的非默認地址的公鑰存放點
4. 如何在本地通過SSH公鑰方式把遠程git倉庫代碼下載到本地:
1.在本地目錄鼠標右鍵選擇 GIT GUI HERE打開GUI窗口選擇克隆已經存在的倉庫如下圖:
2.輸入git倉庫SSH地址上面已經說過
OK,祝您成功,有上面問題可以留言一起討論!