|
在提交了若干更新之后,又或者克隆了某个项目,想回顾下提交历史,可以使用 git log 命令查看。
接下来的例子会用我专门用于演示的 simplegit 项目,运行下面的命令获取该项目源代码:
- git clone git://github.com/schacon/simplegit-progit.git
复制代码
然后在此项目中运行 git log,应该会看到下面的输出:
- $ git log
- commit ca82a6dff817ec66f44342007202690a93763949
- Author: Scott Chacon <[email protected]>
- Date: Mon Mar 17 21:52:11 2008 -0700
-
- changed the version number
-
- commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
- Author: Scott Chacon <[email protected]>
- Date: Sat Mar 15 16:40:33 2008 -0700
-
- removed unnecessary test code
-
- commit a11bef06a3f659402fe7563abf99ad00de2209e6
- Author: Scott Chacon <[email protected]>
- Date: Sat Mar 15 10:31:28 2008 -0700
-
- first commit
-
复制代码
默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面。看到了吗,每次更新都有一个 SHA-1 校验和、作者的名字和电子邮件地址、提交时间,最后缩进一个段落显示提交说明。
git log 有许多选项可以帮助你搜寻感兴趣的提交,接下来我们介绍些最常用的。
我们常用 -p 选项展开显示每次提交的内容差异,用 -2 则仅显示最近的两次更新:
- $ git log -p -2
- commit ca82a6dff817ec66f44342007202690a93763949
- Author: Scott Chacon <[email protected]>
- Date: Mon Mar 17 21:52:11 2008 -0700
-
- changed the version number
-
- diff --git a/Rakefile b/Rakefile
- index a874b73..8f94139 100644
- --- a/Rakefile
- +++ b/Rakefile
- @@ -5,5 +5,5 @@ require 'rake/gempackagetask'
- spec = Gem::Specification.new do |s|
- s.name = "simplegit"
- - s.version = "0.1.0"
- + s.version = "0.1.1"
- s.author = "Scott Chacon"
- s.email = "[email protected]
-
- commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
- Author: Scott Chacon <[email protected]>
- Date: Sat Mar 15 16:40:33 2008 -0700
-
- removed unnecessary test code
-
- diff --git a/lib/simplegit.rb b/lib/simplegit.rb
- index a0a60ae..47c6340 100644
- --- a/lib/simplegit.rb
- +++ b/lib/simplegit.rb
- @@ -18,8 +18,3 @@ class SimpleGit
- end
-
- end
- -
- -if $0 == __FILE__
- - git = SimpleGit.new
- - puts git.show
- -end
- \ No newline at end of file
复制代码
该选项除了显示基本信息之外,还在附带了每次 commit 的变化。当进行代码审查,或者快速浏览某个搭档提交的 commit 的变化的时候,这个参数就非常有用了。
某些时候,单词层面的对比,比行层面的对比,更加容易观察。Git 提供了 --word-diff 选项。我们可以将其添加到 git log -p 命令的后面,从而获取单词层面上的对比。在程序代码中进行单词层面的对比常常是没什么用的。不过当你需要在书籍、论文这种很大的文本文件上进行对比的时候,这个功能就显出用武之地了。下面是一个简单的例子:
- $ git log -U1 --word-diff
- commit ca82a6dff817ec66f44342007202690a93763949
- Author: Scott Chacon <[url=mailto:[email protected]][email protected][/url]>
- Date: Mon Mar 17 21:52:11 2008 -0700
-
- changed the version number
-
- diff --git a/Rakefile b/Rakefile
- index a874b73..8f94139 100644
- --- a/Rakefile
- +++ b/Rakefile
- @@ -7,3 +7,3 @@ spec = Gem::Specification.new do |s|
- s.name = "simplegit"
- s.version = [-"0.1.0"-]{+"0.1.1"+}
- s.author = "Scott Chacon"
-
复制代码
如你所见,这里并没有平常看到的添加行或者删除行的信息。这里的对比显示在行间。新增加的单词被 {+ +} 括起来,被删除的单词被 [- -] 括起来。在进行单词层面的对比的时候,你可能希望上下文( context )行数从默认的 3 行,减为 1 行,那么可以使用 -U1 选项。上面的例子中,我们就使用了这个选项。
另外,git log 还提供了许多摘要选项可以用,比如 --stat,仅显示简要的增改行数统计:
- $ git log --stat
- commit ca82a6dff817ec66f44342007202690a93763949
- Author: Scott Chacon <[email protected]>
- Date: Mon Mar 17 21:52:11 2008 -0700
-
- changed the version number
-
- Rakefile | 2 +-
- 1 file changed, 1 insertion(+), 1 deletion(-)
-
- commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
- Author: Scott Chacon <[email protected]>
- Date: Sat Mar 15 16:40:33 2008 -0700
-
- removed unnecessary test code
-
- lib/simplegit.rb | 5 -----
- 1 file changed, 5 deletions(-)
-
- commit a11bef06a3f659402fe7563abf99ad00de2209e6
- Author: Scott Chacon <[email protected]>
- Date: Sat Mar 15 10:31:28 2008 -0700
-
- first commit
-
- README | 6 ++++++
- Rakefile | 23 +++++++++++++++++++++++
- lib/simplegit.rb | 25 +++++++++++++++++++++++++
- 3 files changed, 54 insertions(+)
-
复制代码
每个提交都列出了修改过的文件,以及其中添加和移除的行数,并在最后列出所有增减行数小计。 还有个常用的 --pretty 选项,可以指定使用完全不同于默认格式的方式展示提交历史。比如用 oneline 将每个提交放在一行显示,这在提交数很大时非常有用。另外还有 short,full 和 fuller 可以用,展示的信息或多或少有些不同,请自己动手实践一下看看效果如何。
- $ git log --pretty=oneline
- ca82a6dff817ec66f44342007202690a93763949 changed the version number
- 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test code
- a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
复制代码
但最有意思的是 format,可以定制要显示的记录格式,这样的输出便于后期编程提取分析,像这样:
- $ git log --pretty=format:"%h - %an, %ar : %s"
- ca82a6d - Scott Chacon, 11 months ago : changed the version number
- 085bb3b - Scott Chacon, 11 months ago : removed unnecessary test code
- a11bef0 - Scott Chacon, 11 months ago : first commit
复制代码
表 2-1 列出了常用的格式占位符写法及其代表的意义。
%H | 提交对象(commit)的完整哈希字串 | %h | 提交对象的简短哈希字串 | %T | 树对象(tree)的完整哈希字串 | %t | 树对象的简短哈希字串 | %P | 父对象(parent)的完整哈希字串 | %p | 父对象的简短哈希字串 | %an | 作者(author)的名字 | %ae | 作者的电子邮件地址 | %ad | 作者修订日期(可以用 -date= 选项定制格式) | %ar | 作者修订日期,按多久以前的方式显示 | %cn | 提交者(committer)的名字 | %ce | 提交者的电子邮件地址 | %cd | 提交日期 | %cr | 提交日期,按多久以前的方式显示 | %s | 提交说明 |
你一定奇怪作者(author)和提交者(committer)之间究竟有何差别,其实作者指的是实际作出修改的人,提交者指的是最后将此工作成果提交到仓库的人。所以,当你为某个项目发布补丁,然后某个核心成员将你的补丁并入项目时,你就是作者,而那个核心成员就是提交者。我们会在第五章再详细介绍两者之间的细微差别。
用 oneline 或 format 时结合 --graph 选项,可以看到开头多出一些 ASCII 字符串表示的简单图形,形象地展示了每个提交所在的分支及其分化衍合情况。在我们之前提到的 Grit 项目仓库中可以看到:
- $ git log --pretty=format:"%h %s" --graph
- * 2d3acf9 ignore errors from SIGCHLD on trap
- * 5e3ee11 Merge branch 'master' of git://github.com/dustin/grit
- |\
- | * 420eac9 Added a method for getting the current branch.
- * | 30e367c timeout code and tests
- * | 5a09431 add timeout protection to grit
- * | e1193f8 support for heads with slashes in them
- |/
- * d6016bc require time for xmlschema
- * 11d191e Merge branch 'defunkt' into local
-
复制代码
以上只是简单介绍了一些 git log 命令支持的选项。表 2-2 还列出了一些其他常用的选项及其释义。
选项 | 说明[/td]
| -p | 按补丁格式显示每个更新之间的差异。 | --word-diff | 按 word diff 格式显示差异。 | --stat | 显示每次更新的文件修改统计信息。 | --shortstat | 只显示 --stat 中最后的行数修改添加移除统计。 | --name-only | 仅在提交信息后显示已修改的文件清单。 | --name-status | 显示新增、修改、删除的文件清单。 | --abbrev-commit | 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。 | --relative-date | 使用较短的相对时间显示(比如,“2 weeks ago”)。 | --graph | 显示 ASCII 图形表示的分支合并历史。 | --pretty | 使用其他格式显示历史提交信息。可用的选项包括 oneline,short,full,fuller 和 format(后跟指定格式)。 | --oneline | --pretty=oneline --abbrev-commit 的简化用法。 | [/tr]
REF:http://cwiki.ossez.com/pages/viewpage.action?pageId=7045476
|
|