git log
View commit History
After you have submitted several updates, or cloned a project, to review the commit history, you can use the git log
commands to view it.
The following example uses the Simplegit project, which I dedicate for demonstration purposes, to get the project source code by running this command:
git clone git://github.com/schacon/simplegit-progit.git
Then run in this project git log
and you should see the following output:
$ git logcommit ca82a6dff817ec66f44342007202690a93763949Author: Scott Chacon <[email protected]>Date: Mon Mar 17 21:52:11 2008 -0700 changed the version numbercommit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7Author: Scott Chacon <[email protected]>Date: Sat Mar 15 16:40:33 2008 -0700 removed unnecessary test codecommit a11bef06a3f659402fe7563abf99ad00de2209e6Author: Scott Chacon <[email protected]>Date: Sat Mar 15 10:31:28 2008 -0700 first commit
by default, git log
all updates are listed by time of submission, and most recent updates are on top. See, each update has a SHA-1 checksum, the author's name and e-mail address, the time of submission, and the final indentation of a paragraph showing the submission instructions.
git log
There are many options to help you search for interested submissions, so let's introduce some of the most common ones.
We use the -p
option to expand the display of content differences for each commit, with -2
only the last two updates displayed :
$ git log-p-2 Commit Ca82a6dff817ec66f44342007202690a93763949author:scott Chacon <[email protected]>date:mon Mar 17 21:52:11 2008-0700 changed the version numberdiff--git a/rakefile b/rakefileindex a874b73. 8f94139 100644---a/rakefile+++ b/rakefile@@ -5,7 +5,7 @@ require ' rake/gempackagetask ' spec = gem::specification.new do | s|-s.version = "0.1.0" + s.version = "0.1.1" S.author = "Scott Chacon" Commit 085bb3bcb608e1e8451d4b 2432f8ecbe6306e7e7author:scott Chacon <[email protected]>date:sat Mar 16:40:33 2008-0700 removed Unn Ecessary test Codediff--git a/lib/simplegit.rb b/lib/simplegit.rbindex a0a60ae. 47c6340 100644---a/lib/simplegit.rb+++ b/lib/simplegit.rb@@ -18,8 +18,3 @@ class Simplegit end end--if $ = __file__ -git = simplegit.new-puts git.show-end\ No newline at end of file
This option is available when you are doing code review, or if you want to quickly see what changes have been made to the updates submitted by other collaborators. In addition, there are a number of summary options that can be used, such as --stat
, to show only a brief count of incremental row counts:
$ git log--stat commit Ca82a6dff817ec66f44342007202690a93763949author:scott Chacon <[email protected]>date:mon Mar 17 21:52:11 2008-0700 changed the version number Rakefile | 2 +-1 files changed, 1 insertions (+), 1 deletions (-) Commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7author:scott Chacon & Lt [Email protected]>date:sat Mar 16:40:33 2008-0700 removed unnecessary test code LIB/SIMPLEGIT.RB | 5-----1 files changed, 0 insertions (+), 5 deletions (-) Commit A11bef06a3f659402fe7563abf99ad00de2209e6author:scott CHAC On <[email protected]>date:sat Mar 10:31:28 2008-0700 First Commit README | 6 ++++++ Rakefile | +++++++++++++++++++++++ LIB/SIMPLEGIT.RB | +++++++++++++++++++++++++ 3 files changed, insertions (+), 0 deletions (-)
Each submission lists the modified files, as well as the number of rows that were added and removed, and a subtotal of all increment and decrement lines at the end of the list. There is also a common --pretty
option to specify that the commit history be presented in a way that is completely different from the default format. For example, it is useful oneline
to put each commit on a single line , which is very helpful when the number of commits is large. In addition short
, full
and fuller
can be used, the information displayed is more or less different, please do your own hands-on to see how the effect.
$ git log --pretty=onelineca82a6dff817ec66f44342007202690a93763949 changed the version number085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test codea11bef06a3f659402fe7563abf99ad00de2209e6 first commit
But most interestingly format
, you can customize the format of the records to be displayed, so that the output facilitates post-programming extraction analysis, like this:
$ git log --pretty=format:"%h - %an, %ar : %s"ca82a6d - Scott Chacon, 11 months ago : changed the version number085bb3b - Scott Chacon, 11 months ago : removed unnecessary test codea11bef0 - Scott Chacon, 11 months ago : first commit
Table 2-1 lists the commonly used format placeholder notation and the meaning it represents.
选项 说明%H提交对象(commit)的完整哈希字串%h提交对象的简短哈希字串%T树对象(tree)的完整哈希字串%t树对象的简短哈希字串%P父对象(parent)的完整哈希字串%p父对象的简短哈希字串%an作者(author)的名字%ae作者的电子邮件地址%ad作者修订日期(可以用 -date= 选项定制格式)%ar作者修订日期,按多久以前的方式显示%cn提交者(committer)的名字%ce提交者的电子邮件地址%cd提交日期%cr提交日期,按多久以前的方式显示%s提交说明
You must be wondering what is the difference between the author (author) _ and _ (Committer) _, in fact, the author refers to the person who actually made the change, the submitter refers to the person who finally submitted the work to the warehouse. So, when you post a patch for a project, and then a core member incorporates your patch into the project, you are the author, and that core member is the submitter. We'll cover the nuances in the fifth chapter in more detail.
With the oneline or format combination --graph
option, you can see a few more simple graphics with an ASCII string representation at the beginning, and visualize the branching and differentiation of each commit. In the Grit project warehouse we mentioned earlier, we can see:
$ 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
The above is simply an introduction git log
to some of the options supported by the command. Table 2-2 also lists some of the other commonly used options and their interpretations.
选项 说明-p 按补丁格式显示每个更新之间的差异。--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(后跟指定格式)。
Limit output length
In addition to the options for customizing the output format, git log
There are many useful options to limit the length of the output, which is to output only part of the commit information. As we've seen before -2
, it shows only the last two commits, in fact, this is the -<n>
wording of the option, which n
can be any natural number, indicating that only a few recent submissions are displayed. However, we do not use this option in practice, Git will automatically invoke the paging program (less) when outputting all commits, to see that an earlier update simply turns to the next page.
There are also options that are limited by time, such as --since
and --until
. The following command lists all submissions in the last two weeks:
$ git log --since=2.weeks
You can give a variety of time formats, such as specific days ("2008-01-15"), or how long ago ("2 years 1 day 3 minutes ago").
You can also give some search criteria to list the submissions that are eligible. --author
Displays the submission of the specified author with options and --grep
searches for the keywords in the submission description with options. (Note that if you want to have a commit that satisfies both of these options, you must use an --all-match
option.) )
If you only care about the historical submissions of certain files or directories, you can git log
specify their paths at the end of the options. Because it is the option placed in the last position, the --
previous option and the qualified path name are separated by two dashes ().
Table 2-3 also lists other commonly used similar options.
选项 说明-(n)仅显示最近的 n 条提交--since, --after 仅显示指定时间之后的提交。--until, --before 仅显示指定时间之前的提交。--author 仅显示指定作者相关的提交。--committer 仅显示指定提交者相关的提交。
To look at a practical example, if you want to view the Git repository during October 2008, Junio Hamano submitted test scripts (files located in the t/directory of the project), you can use the following query command:
--author=gitster --since="2008-10-01" --before="2008-11-01" --no-merges -- t/5610e3b - Fix testcase failure when extended attributeacd3b9e - Enhance hold_lock_file_for_{update,append}()f563754 - demonstrate breakage of detached checkout wid1a43f2 - reset --hard/read-tree --reset -u: remove un51a94af - Fix "checkout --track -b newbranch" on detacb0ad11e - pull: allow "git pull origin $something:$cur
The Git project has more than 20,000 commits, but when we give the search option, only 6 of the items that meet the criteria are listed.
Use the graphical tools to review your commit history
Sometimes graphical tools are more likely to show changes in historical submissions, and GITK, which is released with Git, is one such tool. It is written in tcl/tk, basically equivalent to git log
the visual version of the command, and git log
all the options available can be used on the GITK. After entering the GITK command in the Project working directory, the interface shown in Figure 2-2 will be launched.
Figure 2-2. GITK's graphical interface
The last half of the window shows the branch ancestor map that was submitted, and the next half of the window shows the specific differences of the current point selection.
Git use detailed (5)--Get log View commit history