1. 创建SSH Key 命令
ssh-keygen -t rsa -C "your_email@example.com"
Your identification has been saved in
/Users/your_user_directory/.ssh/id_rsa.
Your public key has been saved in
/Users/your_user_directory/.ssh/id_rsa.pub.
- id_rsa 文件是私有密钥,
- id_rsa.pub 是公开密钥。
2.添加公开密钥
-
在 GitHub 中添加公开密钥,今后就可以用私有密钥进行认证了。
-
点击右上角的账户,再点击 Settings,再点击SSH and GPG keys,再点击 New SSH key。点击 New SSH
Key 之后,会出现如图3.2的输入框。在 Title 中输入适当的密钥名称。 Key 部分请粘贴 id_rsa.pub
文件里的全部内容。 id_rsa.pub的内容可以用如下方法查看。 -
注意:公开密钥的内容不包括邮箱部分,包含“ssh-rsa”
$ cat ~/.ssh/id_rsa.pub
ssh-rsa 公开密钥的内容 your_email@example.com
- 添加成功之后,创建账户时所用的邮箱会接到一封提示“公共密钥添加完成”的邮件。
- 完成以上设置后,就可以用手中的私人密钥与 GitHub 进行认证和通信了。
$ ssh -T git@github.com
The authenticity of host 'github.com (52.74.223.119)' can't be established.
RSA key fingerprint is fingerprint值.
Are you sure you want to continue connecting (yes/no)? 输入yes
Warning: Permanently added 'github.com,52.74.223.119' (RSA) to the list of known hosts.
Enter passphrase for key '/c/Users/FengXingwei/.ssh/id_rsa':输入密码
- 出现如下结果即为成功。
Hi opensourcefxw! You've successfully authenticated, but GitHub does not provide shell access.
3.使用社区功能
- follow——用户
- watch——仓库
4.创建仓库
- 实际创建一个公开的仓库。点击右上角工具栏里的 New repository图标,创建新的仓库。
- Repository name——仓库的名称
- Description——仓库的说明
- Public ——免费的
- Private——收费的
- Initialize this repository with a README
GitHub 会自动初始化仓库并设置 README 文件,让用户可以立刻clone 这个仓库。如果想向 GitHub 添加手中已有的 Git 仓库,建议不要勾选,直接手动 push。 - Add .gitignore
下方左侧的下拉菜单非常方便,通过它可以在初始化时自动生成 .gitignore 文件。这个设定会帮我们把不需要在 Git 仓库中进行版本管理的文件记录在 .gitignore 文件中,省去了每次根据框架进行设置的麻烦。下拉菜单中包含了主要的语言及框架,选择今后将要使用的即可。由于本书中我们并不使用任何框架,所以不做选择。 - Add a license
右侧的下拉菜单可以选择要添加的许可协议文件。如果这个仓库中包含的代码已经确定了许可协议,那么请在这里进行选择。随后将自动生成包含许可协议内容的 LICENSE 文件,用来表明该仓库内容的许可协议。
5.clone 已有仓库
- 仓库的路径
$ git clone git@github.com:hirocastest/Hello-World.git
- 切换到仓库目录
$ cd Hello-World
- 编写代码
hello_word.php的内容:
<?php
echo "Hello World!";
?>
- 由于 hello_word.php 还没有添加至 Git 仓库,所以显示为 Untracked files。
$ git status
- 将 hello_word.php 提交至仓库
$ git add hello_world.php
$ git commit -m "Add hello world script by php"
- 通过 git add命令将文件加入暂存区,再通过 git commit命令提交。 添加成功后,可以通过 git log命令查看提交日志。
$ git log
- 进行 push
$ git push
- 这样一来代码就在 GitHub 上公开了。不妨实际连接 http://github.com/ 用户名 /Hello-World 查看一下。