一、注册GitHub账号。
1,注册账号。
2,新建一个Repository。
二、为GitHub配置SSH公钥。
1,在本地生成SSH秘钥。
(1)使用如下命令查看本地秘钥。
Yooye-2:my-pro yooye$ ls -al ~/.ssh #查看秘钥的命令
total 8
drwx------ 3 yooye staff 96 11 29 16:38 .
drwxr-xr-x+ 53 yooye staff 1696 3 5 08:33 ..
-rw-r--r-- 1 yooye staff 176 11 29 16:38 known_hosts
【备注】:windows系统需要打开Git Bash进行命令行操作。
(2)通过如下命令,在本地生成秘钥。
Yooye-2:my-pro yooye$ ssh-keygen -t rsa -b 4096 -C "597665748@qq.com" #生成秘钥的命令,这里的邮箱采用github的注册邮箱
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/yooye/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/yooye/.ssh/id_rsa.
Your public key has been saved in /Users/yooye/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:LgvvmB/F/hXsx7m3eA2/IdcT4P77A+6UmEba3A455rU 597665748@qq.com
The key's randomart image is:
+---[RSA 4096]----+
| |
| |
| . |
| . .. . |
| S .o. . |
| + =.*+o.o|
| . o o. @+B=*o|
| = + .+.*o*+*|
| oo= ...Eo*B|
+----[SHA256]-----+
(3)重新查看本地秘钥状态。
Yooye-2:my-pro yooye$ ls -al ~/.ssh #查看秘钥的命令
total 24
drwx------ 5 yooye staff 160 3 5 13:39 .
drwxr-xr-x+ 53 yooye staff 1696 3 5 08:33 ..
-rw------- 1 yooye staff 3243 3 5 13:39 id_rsa #生成的私钥
-rw-r--r-- 1 yooye staff 742 3 5 13:39 id_rsa.pub #生成的公钥
-rw-r--r-- 1 yooye staff 176 11 29 16:38 known_hosts
2,提取本地生成的公钥,并将其配置到github。
(1)提取公钥。
Yooye-2:my-pro yooye$ cd ~/.ssh #【1】进入.ssh文件路径
Yooye-2:.ssh yooye$ cat id_rsa.pub #【2】查看公钥内容
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDSsOLgOj6TQz8G9f8NDMpnVY+省略很多字符 #【3】得到公钥,复制备用
(2)将得到的公钥配置到github。
三、Git与GitHub关联并同步代码
1,获取GitHub上新建的Repository所提供的SSH地址。
2,在本地的my-pro项目目录下,通过如下命令,使得git与github相关联。
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上的内容获取至本地进行同步。
Yooye-2:my-pro yooye$ git fetch codeisfun
4,这个阶段我们可以使用gitk —all查看,会发现我们的git与github在版本流中依旧处于分离状态,需要使用merge方法,将本地的master与线上的master进行合并。
Yooye-2:my-pro yooye$ git merge --allow-unrelated-histories codeisfun/master
5,执行push操作,将本地代码提交至github仓库。
git push --set-upstream codeisfun master
6,刷新github仓库,查看本地内容是否已经成功提交。
四、提交分支到GitHub
git push codeisfun my-new-branch-name //注意这里的codeisfun是merge的时候为远程分支取的代名词