在提交若干更新,或者克隆某个项目之后,可能会查看提交历史

git clone https://github.com/schacon/simplegit-progit
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 10:31:28 2008 -0700

    first commit

默认,git log 会按提交时间列出所有的更新,而最近的更新排在最上面。

git log 提供许多选项可以帮助搜索提交,比如-p选项,可以用来显示每次提交的内容差异。

$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
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,7 +5,7 @@ require 'rake/gempackagetask'
 spec = Gem::Specification.new do |s|
     s.platform  =   Gem::Platform::RUBY
     s.name      =   "simplegit"
-    s.version   =   "0.1.0"
+    s.version   =   "0.1.1"
     s.author    =   "Scott Chacon"
     s.email     =   "schacon@gee-mail.com"
     s.summary   =   "A simple gem for using Git in Ruby code."

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test

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

如果想查看每次提交的简略的统计信息,可以使用--stat 选项

$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon <schacon@gee-mail.com>
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 <schacon@gee-mail.com>
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test

 lib/simplegit.rb | 5 -----
 1 file changed, 5 deletions(-)

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon <schacon@gee-mail.com>
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将每个提交放在一行显示。

$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit

format可以定制要显示的记录格式

$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 6 years ago : changed the version number
085bb3b - Scott Chacon, 6 years ago : removed unnecessary test
a11bef0 - Scott Chacon, 6 years ago : first commit

git log --pretty=format 的常用选项

选项 说明
%H(%h) 提交对象的完整(简单)hash
%T(%t) 树对象的完整(简单)hash
%P(%p) 父对象的完整(简单)hash
%an(%ae) 作者名字(邮件)
%ad(%ar) 作者修订日期
%cn(%ce) 提交者名字(邮件)
%cd(%cr) 提交日期
%s 提交说明

作者是实际做出修改的人,提交者是最后将工作成果提交到仓库的人。

所以,当为某个项目发布补丁时,然后某个核心成员将补丁并入项目,你就是作者,而那个核心成员即使提交者。

当oneline或format与--graph结实

$ 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 的常用选项

选项 说明
-p 按补丁格式显示每个更新之间的差异
--stat 显示每次更新的文件修改统计信息
--shortstat 只显示--stat中最后的行数修改添加移除统计
--name-only 仅在提交信息后显示已修改的文件清单
--name-status 显示新增、修改、删除的文件清单
--abbrev-commit 仅显示SHA-1的前几个字符
--relative-date 使用较短的相对时间显示
--graph 显示ASCII图形表示分支合并历史
--pretty 使用其他格式显示历史提交信息

限制输出长度

除了可以定制格式的选项,git log 还可以限制输出长度,而且可以按照时间限制

$ git log --since=2.weeks

或者是使用--author 选项显示指定作者的提交,用--grep 选项搜索提交说明中的关键字。

-S选项可以列出那些添加或移除某些字符串的提交,如找出添加或移除某一个特定函数的引用的提交

$ git log -Sfunction_name

如果只关心某些文件或目录的历史提交,可以在git log 选项的最后指定它们的路径。

git log的输出选项

选项 说明
-(n) 仅显示最近的n条提交
--since,--after 仅显示指定时间之后的提交
--until,--before 仅显示指定时间之前的提交
--author 仅显示指定作者相关的提交
--committer 仅显示指定提交者相关的提交
--grep 仅显示含指定关键字的提交
-S 仅显示添加或移除某个关键字的提交

查看Git仓库中,2008年10月期间,Junio Hamano提交的但未合并的测试文件

$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
   --before="2008-11-01" --no-merges -- t/
5610e3b - Fix testcase failure when extended attributes are in use
acd3b9e - Enhance hold_lock_file_for_{update,append}() API
f563754 - demonstrate breakage of detached checkout with symbolic link HEAD
d1a43f2 - reset --hard/read-tree --reset -u: remove unmerged new paths
51a94af - Fix "checkout --track -b newbranch" on detached HEAD
b0ad11e - pull: allow "git pull origin $something:$current_branch" into an unborn branch

撤销

有时在提交完之后才发现漏掉几个文件没有添加,或者提交信息写错,此时可以运行--amend 尝试重新提交

$ git commit --amend

这个命令会将暂存区的文件提交。如果自上次提交未做任何修改,那么快照保持不变,而修改的只是提交信息。

$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend

最终你只会有一个提交,而第二次提交将代替第一次提交的结果。

取消暂存的文件

如果,修改了两个文件,并且希望作为两次独立的需改提交。但是意外的输入git add * ,取消暂存CONTRIBUTING.md

$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M    CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    README.md -> README

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

    modified:   CONTRIBUTING.md

此时CONTRIBUTING.md 已经是修改未暂存的状态。

虽然在调用时添加--hard 选项,可能导致工作目录所有当前进度丢失。但不加选项的调用,则只会修改暂存区域。

撤销对文件的修改

如果不想保留对CONTRIBUTING.md ,可以检出该文件

$ git checkout -- CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

    renamed:    README.md -> README

git checkout -- [file]是危险的命令,它会拷贝一个文件覆盖当前文件。

results matching ""

    No results matching ""