二、git导入和创建github账户
什么是版本管理 版本管理就是管理更新的历史记录。它为我们提供了一些在软件开发过程中必不可少的功能,例如记录
什么是版本管理
版本管理就是管理更新的历史记录。它为我们提供了一些在软件开发过程中必不可少的功能,例如记录一款软件添加或更改源代码的过程,回滚到特定阶段,恢复误删除的文件等。
初始设置
设置姓名和邮箱地址
首先来设置使用 Git 时的姓名和邮箱地址。名字请用英文输入
$ git config --global user.name "Firstname Lastname"
$ git config --global user.email "your_email@example.com"
config 配置有system级别 global(用户级别) 和local(当前仓库)三个设置先从system-》global-》local 底层配置会覆盖顶层配置 分别使用--system/global/local 可以定位到配置文件
查看当前配置
// 查看系统config
git config --system --list
// 查看当前用户(global)配置
git config --global --list
// 查看当前仓库配置信息
git config --local --list
// 查看所有的配置信息,依次是系统级别、用户级别、仓库级别
git config -l
设置头像
在 GitHub 上随处可见的头像(账户独有的标识)是通过 GravatarA服务显示的。
只要使用创建 GitHub 账户时注册的邮箱在 Gravatar 上设置头像,GitHub 的头像就会变成您设置好的样子。
头像并不是使用 GitHub 时的硬性要求,但如果为代码配上编码者的相貌或标识,会让人觉得安心,同时还可能让对方对您产生兴趣。
设置 SSH Key
GitHub 上连接已有仓库时的认证,是通过使用了 SSH 的公开密钥认证方式进行的。
现在让我们来创建公开密钥认证所需的 SSH Key,并将其添加至 GitHub。
运行下面的命令创建 SSH Key
$ ssh-keygen -t rsa -C "your_email@example.com"
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: 再次输入密码
“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.
The key fingerprint is:
fingerprint值 your_email@example.com
The key's randomart image is:
+--[ RSA 2048]----+
| .+ + |
| = o O . |
略
id_rsa 文件是私有密钥,id_rsa.pub 是公开密钥。
添加公开密钥
在 GitHub 中添加公开密钥,今后就可以用私有密钥进行认证了。
点击右上角的账户设定按钮(Account Settings),选择 SSH Keys 菜单。点击 Add SSH Key 之后,会出现如图 3.2 的输入框。
在 Title 中输入适当的密钥名称。Key 部分请粘贴 id_rsa.pub文件里的内容。
id_rsa.pub的内容可以用如下方法查看:
$ cat ~/.ssh/id_rsa.pub
ssh-rsa 公开密钥的内容 your_email@example.com
添加成功之后,创建账户时所用的邮箱会接到一封提示“公共密钥添加完成”的邮件。
创建仓库
实际创建一个公开的仓库。点击右上角工具栏里的 New repository 图标,创建新的仓库。
- Repository name
在 Repository name 栏中输入仓库的名称。这里我们输入 Hello-World。
- Description
Description 栏中可以设置仓库的说明。这一栏不是必需项,可以留空。
- Public、Private
在这一栏可以选择 Public 还是 Private。public 是将这个仓库公开,private 是将这个仓库不公开。
- Initialize this repository with a README
在 Initialize this repository with a README 选项上打钩,随后GitHub 会自动初始化仓库并设置 README 文件,让用户可以立刻clone 这个仓库。如果想向 GitHub 添加手中已有的 Git 仓库,建议不要勾选,直接手动 push。
- Add .gitignore
下方左侧的下拉菜单非常方便,通过它可以在初始化时自动生成 .gitignore 文件 A。这个设定会帮我们把不需要在 Git 仓库中进行版本管理的文件记录在 .gitignore 文件中,省去了每次根据框架进行设置的麻烦。下拉菜单中包含了主要的语言及框架,选择今后将要使用的即可。一般情况下不做选择。
- Add a license
右侧的下拉菜单可以选择要添加的许可协议文件。如果这个仓库中包含的代码已经确定了许可协议,那么请在这里进行选择。随后将自动生成包含许可协议内容的 LICENSE 文件,用来表明该仓库内容的许可协议。
输入选择都完成后,点击 Create repository 按钮,完成仓库的创建。
连接仓库
下面这个 URL 便是刚刚创建的仓库的页面。
https://github.com/用户名/Hello-Word
公开代码
- clone 已有代码
首先将已有仓库 clone 到身边的开发环境中。
$ git clone git@github.com:hirocastest/Hello-World.git
Cloning into 'Hello-World'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
$ cd Hello-World
这里会要求输入 GitHub 上设置的公开密钥的密码。认证成功后,仓库便会被 clone 至仓库名后的目录中。将想要公开的代码提交至这个仓库再 push 到 GitHub 的仓库中,代码便会被公开。
- 编写代码
这里我们编写一个 hello_world.php 文件,用来输出“Hello World!”。
hello_word.php的内容:
<?php
echo "Hello World!";
?>
由于 hello_word.php 还没有添加至 Git 仓库,所以显示为 Untracked files。
$ git status
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# hello_world.php
nothing added to commit but untracked files present (use "git add" to track)
- 提交
将 hello_word.php 提交至仓库。这样一来,这个文件就进入了版本管理系统的管理之下。今后的更改管理都交由 Git 进行。
$ git add hello_world.php
$ git commit -m "Add hello world script by php"
[master d23b909] 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 d23b909caad5d49a281480e6683ce3855087a5da
Author: hirocastest <hohtsuka@gmail.com>
Date: Tue May 1 14:36:58 2012 +0900
Add hello world script by php
略
- 进行 push
之后只要执行 push,GitHub 上的仓库就会被更新。
$ git push
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 328 bytes, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:hirocastest/Hello-World.git
46ff713..d23b909 master -> master