$ git log
该命令可用于查看项目的提交历史,可以使用项目GitTest为例进行试验,首先克隆源代码到本地:
$ git clone https://github.com/fancyop/GitTest.git
下载完成在此项目中直接运行 git log,默认不用任何参数的话,git log 会按提交时间列出所有的更新,最近的更新排在最上面,可以看到:
$ git log
commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD)
Author: fancyop <fancyop@qq.com>
Date: Wed May 22 15:59:08 2019 +0800
modified README.md
commit e5dd834bc08e9220bd110054e79345ed917972ce
Merge: ce8bfb6 61369e9
Author: fancyop <fancyop@qq.com>
Date: Wed May 22 15:55:40 2019 +0800
Merge branch 'testBranch2'
......
(1)常用参数
git log [-参数]
A、不同信息输出(点击可跳转到相应示例代码)
[--stat]:用来显示每次提交的下面列出所有被修改过的文件、有多少文件被修改了以及被修改过的文件的哪些行被移除或是添加了。 在每次提交的最后还有一个总结。
[--shortstat]:只显示 --stat 中最后的行数修改添加移除统计。
[--name-only]:仅在提交信息后显示已修改的文件清单。
[--name-status]:显示新增、修改、删除的文件清单。
[--abbrev-commit]:仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。
[--relative-date]:使用较短的相对时间显示(比如,“2 weeks ago”)。
[--pretty]:这个参数可以指定使用不同于默认格式的方式展示提交历史。 这个选项有一些内建的子选项供你使用。
[--pretty=oneline]:比如用 oneline 将每个提交放在一行显示,查看的提交数很大时非常有用。
[--pretty=short]、[--pretty=full] 、 [--pretty=fuller] 等展示的信息或多或少有些不同。
下面表格是format可选的常用选项:
选项 | 说明 |
---|---|
|
提交对象(commit)的完整哈希字串 |
|
提交对象的简短哈希字串 |
|
树对象(tree)的完整哈希字串 |
|
树对象的简短哈希字串 |
|
父对象(parent)的完整哈希字串 |
|
父对象的简短哈希字串 |
|
作者(author)的名字 |
|
作者的电子邮件地址 |
|
作者修订日期(可以用 --date= 选项定制格式) |
|
作者修订日期,按多久以前的方式显示 |
|
提交者(committer)的名字 |
|
提交者的电子邮件地址 |
|
提交日期 |
|
提交日期,按多久以前的方式显示 |
|
提交说明 |
[--graph]:可以用来形象的展示分支、合并历史,经常和参数--pretty中的值oneline和format选项结合使用。
B、筛选输出
[-(n)]:如数字n为2时,即命令是 git log -2,用来显示最近两次的提交。
[--since], [--after]:仅显示指定时间之后的提交。
[--until], [--before]:仅显示指定时间之前的提交。
最后一个很实用的 git log 选项是路径(path), 如果只关心某些文件或者目录的历史提交,可以在 git log 选项的最后指定它们的路径。 因为是放在最后位置上的选项,所以用两个短划线(--)隔开之前的选项和后面限定的路径名。
(2)命令示例及相应输出(点击可跳转到相应示例代码)
$ git log -p
commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD)
Author: fancyop <fancyop@qq.com>
Date: Wed May 22 15:59:08 2019 +0800
modified README.md
diff --git a/README.md b/README.md
index c062db5..4ad5ef9 100644
--- a/README.md
+++ b/README.md
@@ -1,2 +1 @@
-# first version
-# add hello.c
No newline at end of file
+# master
...
$ git log --stat
commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD)
Author: fancyop <fancyop@qq.com>
Date: Wed May 22 15:59:08 2019 +0800
modified README.md
README.md | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
commit e5dd834bc08e9220bd110054e79345ed917972ce
Merge: ce8bfb6 61369e9
Author: fancyop <fancyop@qq.com>
Date: Wed May 22 15:55:40 2019 +0800
Merge branch 'testBranch2'
commit ce8bfb699410e8886aa04f4dc48396d2771fd415
Author: fancyop <fancyop@qq.com>
Date: Wed May 22 15:43:26 2019 +0800
delete test2.txt
test2.txt | 1 -
1 file changed, 1 deletion(-)
...
$ git log --shortstat commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD) Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:59:08 2019 +0800 modified README.md 1 file changed, 1 insertion(+), 2 deletions(-) commit e5dd834bc08e9220bd110054e79345ed917972ce Merge: ce8bfb6 61369e9 Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:55:40 2019 +0800 Merge branch 'testBranch2'
...
$ git log --name-only commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD) Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:59:08 2019 +0800 modified README.md README.md commit e5dd834bc08e9220bd110054e79345ed917972ce Merge: ce8bfb6 61369e9 Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:55:40 2019 +0800 Merge branch 'testBranch2'
...
$ git log --name-status commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD) Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:59:08 2019 +0800 modified README.md M README.md commit e5dd834bc08e9220bd110054e79345ed917972ce Merge: ce8bfb6 61369e9 Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:55:40 2019 +0800 Merge branch 'testBranch2'
...
$ git log --abbrev-commit commit 36924e5 (HEAD -> master, origin/master, origin/HEAD) Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:59:08 2019 +0800 modified README.md commit e5dd834 Merge: ce8bfb6 61369e9 Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:55:40 2019 +0800 Merge branch 'testBranch2'
...
$ git log --relative-date commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD) Author: fancyop <fancyop@qq.com> Date: 3 hours ago modified README.md commit e5dd834bc08e9220bd110054e79345ed917972ce Merge: ce8bfb6 61369e9 Author: fancyop <fancyop@qq.com> Date: 3 hours ago Merge branch 'testBranch2'
...
$ git log --pretty=oneline
36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD) modified README.md
e5dd834bc08e9220bd110054e79345ed917972ce Merge branch 'testBranch2'
ce8bfb699410e8886aa04f4dc48396d2771fd415 delete test2.txt
61369e94dbc4ab8716ba8a148f3397dc312b70eb (origin/testBranch) copy sample.png to sample_copy.png, add sample_copy.png
124bf7f54d725770290c8e6bc08aab69fba4ca1d add test2.txt file
84d85f06fadad841de747ac29b09476dd9b339b7 add xxx.c file
1b5352774fea376d8026f5455a7f0933c68aff5a delete test.txt
2fa98d0f1460ac88d7587b5e1f280ea553391c39 add sample.png
85c0902fc3fb2d039902d27f95d254d846a14877 add test.txt
c42e19c08c515e72c45f9b32e63200c22f3e06fb add printGood function
7b2d304e6aea5ab89d86e5e13b0653db3a47ab5e first version
$ git log --pretty=short
commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD)
Author: fancyop <fancyop@qq.com>
modified README.md
commit e5dd834bc08e9220bd110054e79345ed917972ce
Merge: ce8bfb6 61369e9
Author: fancyop <fancyop@qq.com>
Merge branch 'testBranch2'
commit ce8bfb699410e8886aa04f4dc48396d2771fd415
Author: fancyop <fancyop@qq.com>
delete test2.txt
commit 61369e94dbc4ab8716ba8a148f3397dc312b70eb (origin/testBranch)
Author: fancyop <fancyop@qq.com>
copy sample.png to sample_copy.png, add sample_copy.png
...
$ git log --pretty=full
commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD)
Author: fancyop <fancyop@qq.com>
Commit: fancyop <fancyop@qq.com>
modified README.md
commit e5dd834bc08e9220bd110054e79345ed917972ce
Merge: ce8bfb6 61369e9
Author: fancyop <fancyop@qq.com>
Commit: fancyop <fancyop@qq.com>
Merge branch 'testBranch2'
commit ce8bfb699410e8886aa04f4dc48396d2771fd415
Author: fancyop <fancyop@qq.com>
Commit: fancyop <fancyop@qq.com>
delete test2.txt
commit 61369e94dbc4ab8716ba8a148f3397dc312b70eb (origin/testBranch)
Author: fancyop <fancyop@qq.com>
Commit: fancyop <fancyop@qq.com>
copy sample.png to sample_copy.png, add sample_copy.png
...
$ git log --pretty=fuller
commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD)
Author: fancyop <fancyop@qq.com>
AuthorDate: Wed May 22 15:59:08 2019 +0800
Commit: fancyop <fancyop@qq.com>
CommitDate: Wed May 22 15:59:08 2019 +0800
modified README.md
commit e5dd834bc08e9220bd110054e79345ed917972ce
Merge: ce8bfb6 61369e9
Author: fancyop <fancyop@qq.com>
AuthorDate: Wed May 22 15:55:40 2019 +0800
Commit: fancyop <fancyop@qq.com>
CommitDate: Wed May 22 15:55:40 2019 +0800
Merge branch 'testBranch2'
commit ce8bfb699410e8886aa04f4dc48396d2771fd415
Author: fancyop <fancyop@qq.com>
AuthorDate: Wed May 22 15:43:26 2019 +0800
Commit: fancyop <fancyop@qq.com>
CommitDate: Wed May 22 15:43:26 2019 +0800
delete test2.txt
$ git log --pretty=format:"%h - %an, %ar : %s"
36924e5 - fancyop, 2 hours ago : modified README.md
e5dd834 - fancyop, 2 hours ago : Merge branch 'testBranch2'
ce8bfb6 - fancyop, 2 hours ago : delete test2.txt
61369e9 - fancyop, 2 hours ago : copy sample.png to sample_copy.png, add sample_copy.png
124bf7f - fancyop, 2 hours ago : add test2.txt file
84d85f0 - fancyop, 2 hours ago : add xxx.c file
1b53527 - fancyop, 3 hours ago : delete test.txt
2fa98d0 - fancyop, 3 hours ago : add sample.png
85c0902 - fancyop, 3 hours ago : add test.txt
c42e19c - fancyop, 3 hours ago : add printGood function
7b2d304 - fancyop, 6 hours ago : first version
$ git log --pretty=format:"%h %s" --graph
* 36924e5 modified README.md
* e5dd834 Merge branch 'testBranch2'
|
| * 61369e9 copy sample.png to sample_copy.png, add sample_copy.png
| * 84d85f0 add xxx.c file
* | ce8bfb6 delete test2.txt
* | 124bf7f add test2.txt file
|/
* 1b53527 delete test.txt
* 2fa98d0 add sample.png
* 85c0902 add test.txt
* c42e19c add printGood function
* 7b2d304 first version
$ git log -2 commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD) Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:59:08 2019 +0800 modified README.md commit e5dd834bc08e9220bd110054e79345ed917972ce Merge: ce8bfb6 61369e9 Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:55:40 2019 +0800 Merge branch 'testBranch2'
$ git log --since=4.hour $ git log --since=2.week $ git log --after="2019-05-21" $ git log --after="2 years 1 day 3 minutes ago"
$ git log --until=4.hour $ git log --until=2.week $ git log --before="2019-05-21" $ git log --before="2 years 1 day 3 minutes ago"
$ git log --author fancyop
$ git log --committer fancyop
$ git log --grep=first commit 7b2d304e6aea5ab89d86e5e13b0653db3a47ab5e Author: fancyop <fancyop@qq.com> Date: Wed May 22 11:39:24 2019 +0800 first version
$ git log -S add commit 36924e519cf540f1f2c30517cd43dcfad3d03619 (HEAD -> master, origin/master, origin/HEAD) Author: fancyop <fancyop@qq.com> Date: Wed May 22 15:59:08 2019 +0800 modified README.md commit 7b2d304e6aea5ab89d86e5e13b0653db3a47ab5e Author: fancyop <fancyop@qq.com> Date: Wed May 22 11:39:24 2019 +0800 first version