第3篇 使用GitHub的前期准备

3.1使用前的准备

  • 创建GitHub账户

首先需要创建GitHub账户,GitHub官网

  • 设置SSH Key

GitHub上连接已有仓库时的认证,是通过使用了SSH的公开密钥认证方式进行的。现在让我们来创建公开密钥认证所需的SSH Key,并将其添加至GitHub。

运行下面的命令创建SSH Key。

$ ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key
(/Users/your_user_directory/.ssh/id_rsa): 按回车键
Enter passphrase(empty for no passphrase): 输入密码
Enter same passphrase again: 再次输入密码

[email protected]”的部分请改写成你在创建账户时使用的邮箱地址。密码需要在认证时输入,请选择复杂度高并且容易记忆的组合。

输入密码后会出现以下结果:

Your identification has been saved in /c/Users/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:9gQCLiJSs0RaiVajx+sq1TsGLcszfCbyquIH/hVdd1s [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|..=o.            |
|o*oo..           |
|*.ooo . .. . . E |
|=o+o. ..... . o  |
|.+.= . .S .  .   |
| .B o .. o       |
|.=.= o    .      |
|+.*.=            |
|*oo= .           |
+----[SHA256]-----+

其中,id_rsa文件是私有密钥, id_rsa.pub是公开密钥。

  • 添加公开密钥

在GitHub中添加公开密钥,今后就可以用私有密钥进行认证了。

点击右上角的账户设定按钮(Settings),选择SSH and GPG keys 菜单后,就会出现Title和Key两个输入框。在Title中输入适当的密钥名称。Key部分请粘贴id_rsa.pub文件里的内容。id_rsa.pub的内容可以使用如下方法来查看:

$ cat ~/.ssh/id_rsa.pub
ssh-rsa 公开密钥的内容 [email protected]

添加成功后,创建账户时所用的邮箱会收到一封提示“公共密钥添加完成”的邮件([GitHub] A new public key was added to your account)。

完成以上的设置后,就可以用手中的私人密钥与GitHub进行认证和通信了。让我们来尝试一下:

$ ssh -T [email protected]
The authenticity of host 'github.com (192.32.254.132)' can't be established.
RSA key fingerprint is SHA256:nThbg6kyjpJWGl7E1hGOCsdRobTxdCaARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.34.233.212' (RSA) to the list of known hosts.
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa':
Hi harrytsz! You've successfully authenticated, but GitHub does not provide shell access.
(出现此条信息即为连接成功!)
  • 创建仓库

实际创建一个公开的仓库。点击右上角工具栏里的New repository图标,创建新的仓库,如下图所示:

Repository name: 在Repository name栏中输入仓库的名称。

Description:Description栏中可以设置仓库的说明,这一栏不是必需项,可以留空。

Public、Private:在这一栏中可以选择Public或者Private。通常情况下默认Public,创建公开仓库,仓库内的所有内容都会被公开。选择Private可以创建非公开的仓库,用户可以设置访问权限,但这项服务是收费的。

Initialize this repository with a README:在这一选项上打钩,随后GitHub会自动初始化仓库并设置README文件,让用户可以立刻clone这个仓库。如果想向GitHub添加手中已有的Git仓库,建议不要勾选,直接手动push。

Add.gitignore:下方左侧的下拉菜单非常方便,通过它可以在初始化时自动生成.gitignore文件。这个设定会帮助我们把不需要在Git仓库中进行版本管理的文件记录在.gitignore文件中,省去了每次根据框架进行设置的麻烦。下拉菜单中包含了主要的语言及框架,选择今后将要使用的即可。

Add a license:右侧的下拉菜单可以选择要添加的许可协议文件。如果这个仓库中包含的代码已经确定了许可协议,那么请在这里进行选择。随后将自动生成包含许可协议内容的LICENSE文件,用来表明该仓库内容的许可协议。

输入选择都完成后,点击 Create repository按钮,完成仓库的创建。

  • 连接仓库

下面这个URL就是刚刚创建的仓库的页面:

https://github.com/用户名/Hello-World

README.md:在初始化时已经生成好了,README.md文件的内容会自动显示在仓库的首页当中。因此,人们一般会在这个文件中表明本仓库所包含的软件的概要、使用流程、许可协议等信息。

  • 公开代码

clone已有仓库:接下来我们将尝试在已有仓库中添加代码并加以公开。首先将已有仓库clone到身边的开发环境中。clone时指定的路径请参考下图:

$ git clone [email protected]:harrytsz/Hello-World.git
Cloning into 'Hello-World'...
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa':
warning: You appear to have cloned an empty repository.

$ cd Hello-World

这里会要求输入GitHub上设置的公开密钥的密码。认证成功后,仓库便会被clone至仓库名后的目录中。将想要公开的代码提交至这个仓库再push到Github的仓库中,代码便会被公开。

  • 编写代码

这里我们编写一个hello_world.php文件,用来输出“Hello World!”

<?php
echo "Hello World!"
?>

由于hello_world.php文件还没有添加至Git仓库,所以显示为Untracked files。

  • 提交

将hello_world.php提交至仓库。这样一来,这个文件就进入了版本管理系统的管理之下。今后的更改管理都交由Git进行。

$ git add hello_world.php

$ git commit -m "Add hello world script by php"
[master (root-commit) 5f35f2d] Add hello world script by php
 1 file changed, 3 insertions(+)
 create mode 100644 hello_world.php

通过git add命令将文件加入暂存区,再通过git commit命令提交。

添加成功后,可以通过git log命令查看提交日志。

$ git log
commit 5f35f2df09c4979f4ecf2250498ba5dddf499c87 (HEAD -> master)
Author: harrytsz <[email protected]>
Date:   Thu Aug 2 16:58:18 2018 +0800

    Add hello world script by php
  • 进行push

之后只要执行push,GitHub上的仓库就会被更新。

$ git push
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa':
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 258 bytes | 258.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To github.com:harrytsz/Hello-World.git
 * [new branch]      master -> master

这样一来代码就在GitHub上公开了。不妨实际连接http://github.com/用户名/Hello-World查看一下。

#以上内容均摘至《GitHub入门与实践》#

猜你喜欢

转载自blog.csdn.net/Harrytsz/article/details/81359510