生成新的ssh密钥

1
2
ssh-keygen -t rsa -C "youremail@example.com"
#接下来会让你给密钥文件命名,添加密钥文件名前缀:id_rsa_<host名称>

config文件配置

1
2
3
4
5
6
7
8
9
10
11
12
13
# booksnitch数字花园
Host booksnitch
Hostname ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_rsa_booksnitch

# bookfere
Host readfere
HostName ssh.github.com
Port 443
User git
IdentityFile ~/.ssh/id_rsa_readfere

GitHub添加公钥连接和本地互联

进入GitHub后台:
settings-SSH and GPG keys-SSH keys 添加在第一步生成的ssh密钥的公钥,也就是<host名称>.pub中的内容

在本地终端中输入命令查看连接状态:

1
2
3
ssh -T git@readfere
# 出现以下提示,证明连接成功!
# Hi Readfere! You've successfully authenticated, but GitHub does not provide shell access.

将项目库克隆到本地:

1
2
3
# 说明 git@host(host为自定义名称)要不然不生效
git clone git@<host名称>:<GitHub用户名>/<repo名称>.git
# 格式如:git clone git@host_name:git_name/prepo_name.git

本地改动push到远端

1
2
3
4
5
6
7
8
git add .
# 这个命令用于将所有未追踪(untracked)或已修改(modified)的文件添加到 Git 的暂存区(staging area)。. 代表当前目录,而 git add . 会递归地将所有变更的文件添加到暂存区,准备进行提交。

git commit -m "message"
# 这个命令用于将已经暂存在暂存区的文件提交到本地仓库。-m 选项后面的 "message" 是对本次提交的简短描述,用于解释提交的目的或变更。

git push origin <远程分支名>
# 这个命令将本地的提交推送到远程仓库。origin 是远程仓库的默认名称,<远程分支名> 是您要推送到的远程分支的名称。这将同步您的本地提交到远程仓库,使得两者保持一致。

其他可能需要用到的命令

1
2
3
4
5
6
7
8
git remote set-url origin git://new.url.here
# 此命令用于将名为 "origin" 的远程存储库的 URL 更改为指定的新 URL,在 Git 中,“origin” 通常是对最初克隆的远程存储库的默认命名。(修改远程仓库URL)

git remote remove origin
# 此命令会从本地存储库中删除名为 "origin" 的远程存储库。它实际上是断开本地存储库与名为 "origin" 的远程存储库之间的连接。

git remote add origin yourRemoteUrl
# 此命令将具有指定 URL(yourRemoteUrl)的新远程存储库添加到本地存储库。它建立了本地存储库与远程存储库之间的连接,使您能够推送和拉取它们之间的更改。

可能会遇到的问题

1
2
3
4
5
6
7
8
9
10
11
12
$ git clone git@github.com:xxxxx/xxxx.git my-awesome-proj
Cloning into 'my-awesome-proj'...
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

$ # This should also timeout
$ ssh -T git@github.com
ssh: connect to host github.com port 22: Connection timed out

$ # but this might work
$ ssh -T -p 443 git@ssh.github.com
Hi xxxx! You've successfully authenticated, but GitHub does not provide shell access.```

解决方法

1
2
3
$ # Override SSH settings

$ vim ~/.ssh/config

编辑config文件,将以下内容加入每个ssh config里面

1
2
3
Host github.com
Hostname ssh.github.com
Port 443

测试连通性

1
2
3
4
5
6
7
8
9
10
11
$ ssh -T git@github.com
Hi xxxxx! You've successfully authenticated, but GitHub does not
provide shell access.

$ git clone git@github.com:xxxxxx/xxxxx.git my-awesome-proj
Cloning into 'my-awesome-proj'...
remote: Enumerating objects: 15, done.
remote: Counting objects: 100% (15/15), done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 15 (delta 0), reused 15 (delta 0), pack-reused 0
Receiving objects: 100% (15/15), 22.90 KiB | 4.58 MiB/s, done.