- 用 Mac 自帶工具,生成 ssh Key
命令行:ssh-keygen -t rsa -C xxxx@github.com
rsa 加密算法
-C 創(chuàng)建賬號(hào)
ssh-keygen -t rsa -C xxxx@qq.com
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/temp/.ssh/id_rsa): /Users/temp/.ssh/id_rsa_xiaobu
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/temp/.ssh/id_rsa_temp.
Your public key has been saved in /Users/temp/.ssh/id_rsa_temp.pub.
The key fingerprint is:
SHA256:9apFioweHNwSwFnL/GKYBlZxJrnbuzumFh3YZK09xII xxxx@github.com
The key's randomart image is:
+---[RSA 3072]----+
| ..*=oo |
| +E+= + |
|.. @ = . |
|.. * B o . . |
| + X + S . . |
| . = O . o . |
| = + . o |
| o = o |
| ..+o+ . |
+----[SHA256]-----+
- 打開(kāi).ssh 文件中 pub 文件復(fù)制,訪(fǎng)問(wèn)https://github.com/settings/keys 點(diǎn)擊添加 New SSH keys,把復(fù)制內(nèi)容填充到下圖中
截屏2021-02-20 16.33.08.png
名稱(chēng)自定義 ,點(diǎn)擊 Add SSH key 即可 - 將 ssh 密鑰添加到 ssh-agent中
ssh-add ~/.ssh/id_rsa_temp
- 測(cè)試:ssh -T git@github.com
Hi xxxx(名稱(chēng))! You've successfully authenticated, but GitHub does not provide shell access.
- 配置 config 進(jìn)入 Mac 根目錄的.ssh 文件中 打開(kāi) config
#個(gè)人賬號(hào)
Host person.github.com
HostName github.com
User person_用戶(hù)名
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_person
#公司賬號(hào)
Host company.github.com
HostName github.com
User company_用戶(hù)名
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_company
6.替換項(xiàng)目倉(cāng)庫(kù)地址
- 查看當(dāng)前項(xiàng)目的遠(yuǎn)程地址
git remote -v
origin https://github.com/xxxx.git (fetch)
origin https://github.com/xxxx.git (push)
- 移除當(dāng)前項(xiàng)目的遠(yuǎn)程地址
git remote remove origin
- 添加當(dāng)前項(xiàng)目的遠(yuǎn)程地址
git remote add origin git@company.github.com:xxxx.git
地址中 company.github.com 對(duì)應(yīng)的是 config 文件中 host 后面內(nèi)容
注意:
- 如果發(fā)現(xiàn)項(xiàng)目不能提交時(shí):
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
git@github.com: Permission denied (publickey)
執(zhí)行 : ssh-add ~/.ssh/id_rsa_temp
- 出現(xiàn) "The current branch master has no upstream branch "
問(wèn)題原因:本地分支沒(méi)有和遠(yuǎn)程分支關(guān)聯(lián)起來(lái)
解決方案:git push --set-upstream origin master (master代表遠(yuǎn)程分支名稱(chēng))
