一、注册GitHub账号。

1,注册账号。
2,新建一个Repository。

二、为GitHub配置SSH公钥。

1,在本地生成SSH秘钥。
(1)使用如下命令查看本地秘钥。

  1. Yooye-2:my-pro yooye$ ls -al ~/.ssh #查看秘钥的命令
  2. total 8
  3. drwx------ 3 yooye staff 96 11 29 16:38 .
  4. drwxr-xr-x+ 53 yooye staff 1696 3 5 08:33 ..
  5. -rw-r--r-- 1 yooye staff 176 11 29 16:38 known_hosts

【备注】:windows系统需要打开Git Bash进行命令行操作。
image.png

(2)通过如下命令,在本地生成秘钥。

  1. Yooye-2:my-pro yooye$ ssh-keygen -t rsa -b 4096 -C "597665748@qq.com" #生成秘钥的命令,这里的邮箱采用github的注册邮箱
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/Users/yooye/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /Users/yooye/.ssh/id_rsa.
  7. Your public key has been saved in /Users/yooye/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:LgvvmB/F/hXsx7m3eA2/IdcT4P77A+6UmEba3A455rU 597665748@qq.com
  10. The key's randomart image is:
  11. +---[RSA 4096]----+
  12. | |
  13. | |
  14. | . |
  15. | . .. . |
  16. | S .o. . |
  17. | + =.*+o.o|
  18. | . o o. @+B=*o|
  19. | = + .+.*o*+*|
  20. | oo= ...Eo*B|
  21. +----[SHA256]-----+

(3)重新查看本地秘钥状态。

  1. Yooye-2:my-pro yooye$ ls -al ~/.ssh #查看秘钥的命令
  2. total 24
  3. drwx------ 5 yooye staff 160 3 5 13:39 .
  4. drwxr-xr-x+ 53 yooye staff 1696 3 5 08:33 ..
  5. -rw------- 1 yooye staff 3243 3 5 13:39 id_rsa #生成的私钥
  6. -rw-r--r-- 1 yooye staff 742 3 5 13:39 id_rsa.pub #生成的公钥
  7. -rw-r--r-- 1 yooye staff 176 11 29 16:38 known_hosts

2,提取本地生成的公钥,并将其配置到github。
(1)提取公钥。

  1. Yooye-2:my-pro yooye$ cd ~/.ssh #【1】进入.ssh文件路径
  2. Yooye-2:.ssh yooye$ cat id_rsa.pub #【2】查看公钥内容
  3. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDSsOLgOj6TQz8G9f8NDMpnVY+省略很多字符 #【3】得到公钥,复制备用

(2)将得到的公钥配置到github。
image.png

三、Git与GitHub关联并同步代码

1,获取GitHub上新建的Repository所提供的SSH地址。
image.png

2,在本地的my-pro项目目录下,通过如下命令,使得git与github相关联。

  1. Yooye-2:my-pro yooye$ git remote add codeisfun git@github.com:Yooye/codeIsFun.git

其中:
(1)codeisfun 是一个自定义的名称,在后面的操作过程中,将指代我们的github仓库。
(2)git@github.com:Yooye/codeIsFun.git 为我们获取到的github提供的ssh地址。

3,遵循先获取后提交的流程,使用fetch将github上的内容获取至本地进行同步。

  1. Yooye-2:my-pro yooye$ git fetch codeisfun

4,这个阶段我们可以使用gitk —all查看,会发现我们的git与github在版本流中依旧处于分离状态,需要使用merge方法,将本地的master与线上的master进行合并。

  1. Yooye-2:my-pro yooye$ git merge --allow-unrelated-histories codeisfun/master

5,执行push操作,将本地代码提交至github仓库。

  1. git push --set-upstream codeisfun master

6,刷新github仓库,查看本地内容是否已经成功提交。

四、提交分支到GitHub

  1. git push codeisfun my-new-branch-name //注意这里的codeisfunmerge的时候为远程分支取的代名词