Git 实际操作之一

来源:互联网 发布:软件的demo片 编辑:程序博客网 时间:2024/06/01 07:11

Git 实际操作之一 - git init / git status / git add / git commit / git log

1. git init - 初始化仓库
        要使用 Git 进行版本管理,必须先初始化仓库。 Git 是使用 git init 命令进行初始化的。实际建立一个目录并初始化仓库。
$ mkdir git-tutorial
$ cd git-tutorial
$ git init

strong@foreverstrong:~$ cd github_work/strong@foreverstrong:~/github_work$ lltotal 12drwxrwxr-x  3 strong strong 4096 Dec 17 14:04 ./drwxr-xr-x 44 strong strong 4096 Dec 17 14:03 ../drwxrwxr-x  4 strong strong 4096 Dec 17 14:52 Hello-World/strong@foreverstrong:~/github_work$ mkdir git-tutorialstrong@foreverstrong:~/github_work$ cd git-tutorial/strong@foreverstrong:~/github_work/git-tutorial$ git initInitialized empty Git repository in /home/strong/github_work/git-tutorial/.git/strong@foreverstrong:~/github_work/git-tutorial$ lltotal 12drwxrwxr-x 3 strong strong 4096 Dec 17 18:46 ./drwxrwxr-x 4 strong strong 4096 Dec 17 18:45 ../drwxrwxr-x 7 strong strong 4096 Dec 17 18:46 .git/strong@foreverstrong:~/github_work/git-tutorial$

        如果初始化成功,执行了 git init 命令的目录下就会生成 .git 目录。这个 .git 目录里存储着管理当前目录内容所需的仓库数据。
        在 Git 中,我们将这个目录的内容称为“附属于该仓库的工作树”。文件的编辑等操作在工作树中进行,然后记录到仓库中,以此管理文件的历史快照。如果想将文件恢复到原先的状态,可以从仓库中调取之前的快照,在工作树中打开。开发者可以通过这种方式获取以往的文件。

2. git status - 查看仓库的状态
        git status 命令用于显示 Git 仓库的状态。
        工作树和仓库在被操作的过程中,状态会不断发生变化。在 Git 操作过程中时常用 git status 命令查看当前状态。我们来实际查看一下当前状态。
$ git status

strong@foreverstrong:~/github_work/git-tutorial$ git statusOn branch masterInitial commitnothing to commit (create/copy files and use "git add" to track)strong@foreverstrong:~/github_work/git-tutorial$ 

        结果显示了我们当前正处于 master 分支下。接着还显示了没有可提交的内容。所谓提交(Commit),是指“记录工作树中所有文件的当前状态”。尚没有可提交的内容,就是说当前我们建立的这个仓库中还没有记录任何文件的任何状态。这里,我们建立 README.md 文件作为管理对象,为第一次提交做前期准备。
$ touch README.md
$ git status

strong@foreverstrong:~/github_work/git-tutorial$ touch README.mdstrong@foreverstrong:~/github_work/git-tutorial$ git statusOn branch masterInitial commitUntracked files:  (use "git add <file>..." to include in what will be committed)README.mdnothing added to commit but untracked files present (use "git add" to track)strong@foreverstrong:~/github_work/git-tutorial$ 

        可以看到在 Untracked files 中显示了 README.md 文件。类似地,只要对 Git 的工作树或仓库进行操作, git status 命令的显示结果就会发生变化。

3. git add - 向暂存区中添加文件
        如果只是用 Git 仓库的工作树创建了文件,那么该文件并不会被记入 Git 仓库的版本管理对象当中。因此我们用 git status 命令查看README.md 文件时,它会显示在 Untracked files 里。
        要想让文件成为 Git 仓库的管理对象,就需要用 git add 命令将其加入暂存区(Stage 或者 Index)中。暂存区是提交之前的一个临时区域。
$ git add README.md
$ git status

strong@foreverstrong:~/github_work/git-tutorial$ git add README.mdstrong@foreverstrong:~/github_work/git-tutorial$ git statusOn branch masterInitial commitChanges to be committed:  (use "git rm --cached <file>..." to unstage)new file:   README.mdstrong@foreverstrong:~/github_work/git-tutorial$ 

        将 README.md 文件加入暂存区后,git status 命令的显示结果发生了变化。可以看到,README.md 文件显示在 Changes to be committed 中了。

4. git commit - 保存仓库的历史记录

        git commit 命令可以将当前暂存区中的文件实际保存到仓库的历史记录中。通过这些记录,我们就可以在工作树中复原文件。


4.1 记述一行提交信息
        我们来实际运行一下 git commit命令。
$ git commit -m "First commit"

strong@foreverstrong:~/github_work/git-tutorial$ git commit -m "First commit"[master (root-commit) 3f44bb6] First commit 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 README.mdstrong@foreverstrong:~/github_work/git-tutorial$ 

        -m 参数后的 "First commit"称作提交信息,是对这个提交的概述。

4.2 记述详细提交信息
        刚才我们只简洁地记述了一行提交信息,如果想要记述得更加详细,请不加 -m,直接执行 git commit命令。执行后编辑器就会启动,并显示如下结果。

strong@foreverstrong:~/github_work/git-tutorial$ git commit

在编辑器中记述提交信息的格式如下:
        第一行:用一行文字简述提交的更改内容
        第二行:空行
        第三行以后:记述更改的原因和详细内容
        只要按照上面的格式输入,今后便可以通过确认日志的命令或工具看到这些记录。

        在以 #(井号)标为注释的 Changes to be committed(要提交的更改)栏中,可以查看本次提交中包含的文件。将提交信息按格式记述完毕后,请保存并关闭编辑器,以 #(井号)标为注释的行不必删除。随后,刚才记述的提交信息就会被提交。


4.3 中止提交
        如果在编辑器启动后想中止提交,请将提交信息留空并直接关闭编辑器,随后提交就会被中止。

4.4 查看提交后的状态
        执行完 git commit命令后再来查看当前状态。
$ git status

strong@foreverstrong:~/github_work/git-tutorial$ git statusOn branch masternothing to commit, working directory clean

5. git log - 查看提交日志
        git log 命令可以查看以往仓库中提交的日志。包括可以查看什么人在什么时候进行了提交或合并,以及操作前后有怎样的差别。
$ git log

strong@foreverstrong:~/github_work/git-tutorial$ git logcommit 3f44bb613b0da5e24655c3ce69cb62e3befe5ae6Author: chengyq116 <chengyq116@163.com>Date:   Sun Dec 17 18:51:14 2017 +0800    First commitstrong@foreverstrong:~/github_work/git-tutorial$ 

        屏幕显示了刚刚的提交操作。commit 栏旁边显示的“3f44bb……”是指向这个提交的哈希值。Git 的其他命令中,在指向提交时会用到这个哈希值。
Author 栏中显示我们给 Git 设置的用户名和邮箱地址。 Date 栏中显示提交执行的日期和时间。再往下就是该提交的提交信息。

5.1 只显示提交信息的第一行
       如果只想让程序显示第一行简述信息,可以在 git log 命令后加上 --pretty=short。这样一来开发人员就能够更轻松地把握多个提交。
$ git log --pretty=short

strong@foreverstrong:~/github_work/git-tutorial$ git log --pretty=shortcommit 3f44bb613b0da5e24655c3ce69cb62e3befe5ae6Author: chengyq116 <chengyq116@163.com>    First commitstrong@foreverstrong:~/github_work/git-tutorial$ 

5.2 只显示指定目录、文件的日志

        只要在 git log 命令后加上目录名,便会只显示该目录下的日志。

        如果加的是文件名,就会只显示与该文件相关的日志。
$ git log README.md

strong@foreverstrong:~/github_work/git-tutorial$ git log README.mdcommit 3f44bb613b0da5e24655c3ce69cb62e3befe5ae6Author: chengyq116 <chengyq116@163.com>Date:   Sun Dec 17 18:51:14 2017 +0800    First commitstrong@foreverstrong:~/github_work/git-tutorial$ 

5.3 显示文件的改动
        如果想查看提交所带来的改动,可以加上 -p 参数,文件的前后差别就会显示在提交信息之后。
$ git log -p

strong@foreverstrong:~/github_work/git-tutorial$ git log -pcommit 3f44bb613b0da5e24655c3ce69cb62e3befe5ae6Author: chengyq116 <chengyq116@163.com>Date:   Sun Dec 17 18:51:14 2017 +0800    First commitdiff --git a/README.md b/README.mdnew file mode 100644index 0000000..e69de29strong@foreverstrong:~/github_work/git-tutorial$ 

        比如,执行下面的命令,就可以只查看 README.md 文件的提交日志以及提交前后的差别。
$ git log -p README.md

strong@foreverstrong:~/github_work/git-tutorial$ git log -p README.mdcommit 3f44bb613b0da5e24655c3ce69cb62e3befe5ae6Author: chengyq116 <chengyq116@163.com>Date:   Sun Dec 17 18:51:14 2017 +0800    First commitdiff --git a/README.md b/README.mdnew file mode 100644index 0000000..e69de29strong@foreverstrong:~/github_work/git-tutorial$ 

        如上所述,git log命令可以利用多种参数帮助开发者把握以往提交的内容。



references
(日) 大塚弘记 著, 支鹏浩, 刘斌 译. GitHub入门与实践[M]. 北京:人民邮电出版社, 2015. 1-255