构建高质量软件:持续集成与持续交付系统实践
上QQ阅读APP看书,第一时间看更新

3.1 快速上手Git

Git是一个开源项目,几乎所有的操作系统都支持Git的安装。我们既可以从Git官网上下载最新的安装包进行安装,也可以使用apt、yum等软件包管理命令进行安装。Git官网下载地址为https://git-scm.com/downloads。本章所列举的Git示例都将基于Ubuntu操作系统(与在Windows下直接使用Git Bash一模一样)。如果你已经安装了Git,或者对Git的安装过程很熟悉,则可以跳过本节,这并不会影响后续章节的学习。安装命令具体如下。

> sudo apt-get update
> sudo apt-get install git -y
//这里省略部分代码。
Selecting previously unselected package git-man.
Preparing to unpack .../git-man_1%3a2.7.4-0ubuntu1.9_all.deb ...
Unpacking git-man (1:2.7.4-0ubuntu1.9) ...
Selecting previously unselected package git.
Preparing to unpack .../git_1%3a2.7.4-0ubuntu1.9_amd64.deb ...
Unpacking git (1:2.7.4-0ubuntu1.9) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up liberror-perl (0.17-1.2) ...
Setting up git-man (1:2.7.4-0ubuntu1.9) ...
Setting up git (1:2.7.4-0ubuntu1.9) ...

使用“sudo apt-get install git -y”命令,很容易就能完成对Git及其依赖包的安装,当Git成功安装后,我们可以通过“git --version”命令检查当前所安装Git的版本,以及通过“git --help”命令获取更多git命令的使用帮助,具体操作如下所示。

#检查Git版本。
> git --version
> git version 2.7.4
#获得git命令的使用帮助。
> git --help
usage: git [--version] [--help] [-C <path>] [-c name=value]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
    clone      Clone a repository into a new directory
    init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
    add        Add file contents to the index
    mv         Move or rename a file, a directory, or a symlink
    reset      Reset current HEAD to the specified state
    rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
    bisect     Use binary search to find the commit that introduced a bug
    grep       Print lines matching a pattern
    log        Show commit logs
    show       Show various types of objects
    status     Show the working tree status

grow, mark and tweak your common history
    branch     List, create, or delete branches
    checkout   Switch branches or restore working tree files
    commit     Record changes to the repository
    diff       Show changes between commits, commit and working tree, etc
    merge      Join two or more development histories together
    rebase     Forward-port local commits to the updated upstream head
    tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
    fetch      Download objects and refs from another repository
    pull       Fetch from and integrate with another repository or a local branch
    push       Update remote refs along with associated objects

当Git软件在Ubuntu操作系统的安装完成之后,我们可以使用git命令创建本地仓库,提交版本变更,具体步骤如下。

1)进入工作目录,命令如下。

> cd /home/wangwenjun/git/lesson01/

2)执行git init命令,创建一个空的Git仓库并初始化,命令如下:

> git init

3)空的Git仓库创建并初始化成功后,工作目录下会出现一个“.git”目录,其结构如图3-1所示。

063-01

图3-1 “.git”目录的结构

Initialized empty Git repository in /home/wangwenjun/git/lesson01/.git/

需要特别说明的一点是,“.git”目录只会出现在工作目录的根目录下(这一点与SVN完全不一样,SVN除了在根目录下创建“.svn”目录之外,还会在所有的子目录下创建“.svn”目录),一般情况下,请不要直接手动修改“.git”目录中的内容,否则就会出现不可恢复的错误,其中的内容应由Git自行维护和管理。

至此,我们只是创建了一个空的Git仓库,并没有添加或提交任何文件。现在需要在工作目录下创建一个文件,然后执行git status命令,以查看工作目标的状态,代码如下。

# 在工作目录中创建一个文件。
> echo "hello git" >a.txt
# 执行git status命令,查看工作目录的状态。
> git status
On branch master
Initial commit
Untracked files:
    (use "git add <file>..." to include in what will be committed)
        a.txt
nothing added to commit but untracked files present (use "git add" to track)

在当前工作目录中执行git status命令,我们将会看到Git并未对a.txt文件进行版本跟踪(a.txt文件的状态是Untracked的),如果此刻删除a.txt文件,则该文件将会无法恢复。若要对a.txt文件进行版本控制,则还要再次执行git add命令。

# 执行git add命令(将变更加入暂存区,后文会讲到)。
> git add a.txt
# 再次执行git status命令,查看工作目录的状态。
> git status
On branch master
Initial commit
Changes to be committed:
    (use "git rm --cached <file>..." to unstage)
        new file:   a.txt

将文件加入暂存区后,就可以通过git commit命令将变更提交至本地仓库了。需要注意的是,首次使用git commit命令时会出现错误提示,原因是当前并未设置提交者的email和name等信息。

#将变更提交至本地仓库。
> git commit --message "first commit"
# 出现错误提示信息。
*** Please tell me who you are.
Run
    git config --global user.email "you@example.com"
    git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

根据Git的错误提示信息和指引,首先需要设置好提交者的email和name信息,然后再次执行提交命令即可成功。

# 设置提交者的邮箱地址。
> git config --local user.email "alex@wangwenjun.com"
# 设置提交者的用户名。
> git config --local user.name "Alex Wang"
# 再次提交变更。
> git commit --message "first commit"
[master (root-commit) f7c6a65] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

如果要获得本地仓库的历史提交记录,就需要执行git log命令来查看(git log命令是常用的命令之一)。关于Git文件的生命周期和三大区域,3.2节将结合实例进行深度剖析,关于Git的配置,3.5节将会进行详细介绍。

# 执行 git log命令查看历史提交记录。
> git log --decorate --graph --oneline --all
* f7c6a65 (HEAD -> master) first commit

至此,Git软件安装完毕。接下来,我们将进一步学习Git的其他常用命令,以及如何通过Git命令创建并初始化一个空的本地仓库,如何将未跟踪的文件纳入Git版本管理并提交至本地仓库。