如果用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
希望这篇博客,能给你带来帮助。
既然都看到这了,就点赞关注留言一下呗,你的支持是对博主最大的鼓励 ^ ^