如果用SSH方法來git clone github repo的話,就得通過以下步驟來做,
博主是以Macbook為例的,如果你是用windows或者linux的,就自己查查怎么安裝吧!
- 檢查本地電腦里有沒有SSH keys
[1] 打開Terminal
[2] 輸入 ls -al ~/.ssh這個命令來檢查是否已經有SSH keys
一般默認情況下的 SSH key 的格式如下:
(1)id_rsa.pub
(2)id_ecdsa.pub
(3)id_ed25519.pub
如果發現都沒 SSH key, 那么就得創建一個新的 SSH key - 創建一個新的SSH key
[1] 打開 Terminal
[2] 輸入這個命令
$ ssh-keygen -t ed25519 -C "your_email@example.com"
提示:
如果你的蘋果電腦系統用的legacy system, 這是不支持 Ed25519的,那么就得使用下面的命令:
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
輸入完上面的任何一個命令之后,就會看到這個結果:
> Generating public/private rsa key pair.
緊接著看到另外一個結果:
> Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
這是默認的存key的地址,直接按’enter’就行了
然后就會看到下面的結果:
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
輸入完之后,就看到你的identification和public key存在的地方,你的key fingerprint也可以知道,和你的key’s randomart image也可以看到
- 將你的SSH key 加入到ssh-agent
[1] 在后臺開始ssh-agent,命令如下:
$ eval "$(ssh-agent -s)"
> Agent pid 59566
提示:這個Agent pid有可能會跟你的不一樣,
[2] 如果你的macOS Sierra 10.12.2 or later,you will need to modify your ~/.ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain.
(a) First, check to see if your ~/.ssh/config file exists in the default location.
$ open ~/.ssh/config
> The file /Users/you/.ssh/config does not exist.
(b) 如果檔案不存在,就得創建一個新的檔案
$ vim ~/.ssh/config
( C ) Open your ~/.ssh/config file, then modify the file to contain the following lines. If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup. 粘貼下面的代碼
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
[3] Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace id_rsa in the command with the name of your private key file. 輸入下面的命令
$ ssh-add -K ~/.ssh/id_rsa
- 添加新的SSH key
[1] 在添加ssh key之前,先檢查,是否已經在本地電腦里有ssh key了
ls -al ~/.ssh
[2] 已經有了ssh key了,就Copy the SSH public key to your clipboard.
If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.
$ pbcopy < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard
[3] 去到 Github -> setting -> SSH and GPG keys -> New SSH key

[4] 將剛剛已經復制好的ssh key,粘貼到 key區域里,不能添加或者減少任何的字符,包括空格

[5] 點擊 Add SSH key按鈕
- 測驗你的SSH Connection
進入到你任何一個repo,嘗試一下git push,有可能你會碰到無法git push, 那么你就得用下面的命令來做git push
git push -f origin main
這樣以后就可以直接
git push origin main
希望這篇博客,能給你帶來幫助,
既然都看到這了,就點贊關注留言一下唄,你的支持是對博主最大的鼓勵 ^ ^
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/294328.html
標籤:其他
下一篇:初始化腳本
